Ayatollah Ruhollah Khomeini, emboldened by an enormous homecoming turnout of millions of jubilant Iranians, warned today that if the goverment refused to capitualte to his revolutionary demands and resign, he would arrest its leaders and appoint a provisional government.

Setting the stage for a critical confrontation with the beleaguered government of Prime Minister Shahpour Bakhtial possibly in the next few days, Khomeini lashed out at absent Shah Mohammed Reza Pahlavi, Iran's parliament, Bakhtiar's fragile regime and the United States -- all as enemies of the people.

In an emotional, 30-minute speech at Tehran's Behesht Zahra Cemetery, where hundreds of victims of the past year's upheaval are buried, Khomeini warned the current leaders he would "shut their mouths" and will appoint a governemtn "with the support of the people."

But Khomeini stopped short of naming a new government immediately, which moderate political leaders had feared, and he made a solicitious appeal to the military to join his cause. All of this left the prospects of an imminent civil war triggered by the Moslem leader's return less certain than once feared.

However, Sadegh Ghotbzadeh, a top Khomeini aide, said the ayatollah will announce a provisional government in two or three days.

That would be followed by a national referendum to abolish the current government, which was appointed by the shah before he left the country Jan. 16, and then an election for a constituent assembly and the formation of an Islamic republic.

Not since Lenin was sent across Germany in a sealed train in 1917 to Petrograd's, Finland Station to lead the Bolshevike against the czar has a revolutionary leader's return to his homeland seemed so full of portent as Khomeini's arrival in Iran.

In an extraordinary display of support, Iranians of all walks of life jammed the broad thoroughfares leading from Mehrabad airport, where Khomeini arrived after a 5 1/2-hour flight from Paris, 18 miles to the cemetery in south Tehran, where he vowed to eliminate all vestiges of the shah's 37 years of authoritarian rule.

The ayatolah's supportes created such immovable masses of people that the 78-year-old Moslem leader was whisked the last three miles in a helicopter.

The helicopter belonged to the Iranian Army, which underscored the collaborative role that segments of the military played in the opposition leader's return.

The army, which in past months has blanketed Tehran with tanks, armored personnel carriers and trucks mounted with machine guns, was conspicuously absent during the day-long hero's welcome.

What soldiers were present seemed to blend into the throngs of people lining the motorcade route and many of them openly participated in the jubilation. Khomeini was protected at the airport by a bodyguard contingent of noncommissioned officers from the Iranian Air Force.

Although there were unofficial crowd estimates of 2 million, it was impossible to calculate the number of Iranians who turned out in this sprawling city. By all appearances, it was larger than any of the massive antigovernment demonstrations of the past six weeks.

As he drove along the cordoned-off route, accompanied by press buses and motorcycle outriders, an ear-splitting roar preceeded the procession as the throung of people caught their first glimpse of the motorcade.

The road had been meticulously washed all along the route by volunteers during the night, and huge colorful banners and portraits of the ayatollah, the Persian word for religious leader, were affixed seemingly to every stationary object.

A new banner that appeared frequently was described by Khomeini's upporters as the new flag of the Islamic republic. It is an emblem of a clenched first on a white background, with three faces to depict political prisoners. Above the symbol in red, to denote the blood of Iran's martyrs, are the words "Allahu Akbar" (God is Great), and underneath in black, "Islamic Republic."

Although he made brief remarks at the airport, in which he bitterly assailed foreign influence in lran, Khomeini saved his most important political statement for the cemetery.

There, he declared: "This is an illegal parliament, and the members stole money from the people and have to pay for it. The government is illegal, the king is illegal. His father is illegal..."

"When the shah left, the people said this government is illegal but Bakhtiar says this government is legal now," Khomeini said.

Sitting in a chair throughout the speech, the turbaned ayatollah was equally critical of U.S. policy in Iran.

Referring obliquely to President Carter's support of the shah and his human rights policy elsewhere, Khomeini said, "Is it human rights to say that when we want to name a government we get a cemetery full of people?"

"We will not let the United States bring the shah back. This is what the shah wants. Wake up. Watch out. They want the country to go back to what it was previously," Khomeini declared.

Khomeini appealed to Iranian Army generals not to be left out of step with histroy and to join the revolutionary cause.

"I advise you to come and be with our people," Khomeini said.

Referring to reported mutinies and defections in the military ranks, Khomeini said, "We thank them all, and those that did not join we tell them they should join."

After Khomeini's airport speech, Bakhtiar told a reporter he would "do my best to have good relations with him, but only on certain conditions that I have outlined."

Bakhtiar has attempted to negotiate with Khomeini to achieve a peaceful soultion to the insurrection but the ayatollah has said he will not talk to the prime minister unless he resigns first.

Bakhtiar said he hopes to talk with Khomeini within the next two days. but added, "That is up to him. I want to explain my position. I hope to explain my position and its basis in legality." The premier added, "I know all the difficult problems we are going to meet in the next few days. I hope we can reach a solution."

After Khomeini finished his cemetery speech he was flown by helicopter to Tehran's Pahlavi Hospital where he attempted to visit antigovernment protesters recently shot by army troops.

But so many supporters crowded the hospital to get a glimpse of Khomeini that he was forced to leave after about 15 minutes He was driven to the home of a supporter, where he prayed and prepared to spend the night.

Khomeini is expected to visit the opposition's headquarters in a converted girl's school near Iran's parliament on Friday.