Ayatollah Rubhollah Khomeini today declared himself military commander-in-chief and gave the country's armed forces 24 hours to crush a Kurdish rebellion or face the wrath of the revolution.

Shortly after his declaration and a government ultimatum to the Kurds to end their revolt, the western Iranian town of Paveh, captured by Kurds this week, was back in the hands of Iranian security forces.

Army troops, state police and Revolutionary Guards sent to the region from throughout Iran occupied the town today after Kurdish tribesmen abandoned their positions and retreated into the mountains.

The government said Paveh was recaptured after fighting in which 400 persons were killed and hundreds more wounded.A government spokesman, Sadeq Tabatabai, announced over the state radio that security forces' losses were 18 dead and 40 wounded.

Deputy Prime Minister Mostafa Chamran, who had been trapped there earlier with the last remnants of beleagured government forces, led a counterattack from inside the town before it fell, the radio said.

Scores were killed and wounded in four days of fighting in the town, following an attack by Kurdish tribesmen early Wednesday morning after they had evacuated their civilian population to nearby mountain camps.

Earlier the government said 40 of the 280 Revolutionary Guards in the town had been killed. The state radio said 18 of them had been beheaded when the tribesmen attacked Paveh's only hospital.

Kurdish losses reportedly also were heavy. The radio said 2,000 tribesmen took part in attack.

A spokesman for the Kurds said tonight the tribesmen retreated because it would have been futile to battle the superior Iranian forces, including the Air Force. He termed the Paveh assult a guerrilla action that showed the Kurds will not permit their rights to be trampled without a fight.

Khomeni's action in bypassing the country's military leadership in his role as "supreme commander" followed a speech last night in which he warned against opposing his government.

Khomeini lashed out at the press, political parties, lawyers and intellectuals. Speaking in the holy city of Qom, he declared he would return to Tehran if necessary to put his Islamic revolution on the correct course and end all opposition.

"If we had been truly revolutionary we would have destroyed the press long before this," Khomeini said, supporting the closing of what he termed "counterrevolutionary newspapers."

He condemned all political parties "to death at the gallows," reiterating the slogan of Moslem militants who have attacked democratic groups in Tehran this week with shouts of: "The only party is the party of God."

"As supreme commander of the armed forces, I direct the chief of staff to order all state police units and the army to the Paveh area and the government to provide transport for Revolutionary Guards," Khomeini said.

He directed the forces to be fully armed and ready for battle and, "without waiting for any further orders," to put down the rebellion.

The ayatollah warned the armed forces they would face the wrath of the revolution if they failed to crush the revolt.

"I hold the armed forces responsible for the massacres in Paveh and if they disobey my command I will deal with them in a revolutionary way," he said.

"They keep telling me from the Paveh area that the government and the Army have done nothing. If within 24 hours something positive is not achieved, I will hold the Army chief and head of the state police responsible," Khomeini added.

He issued a ultimatum, which expired at 1 p.m. warning the Kurds that if they did not end their rebellion, troops and Revolutionary Guards from throughout Iran would destroy them.

Kurdish leaders condemned the government action to crush the revolt. Kurdish spiritual leader, Sheik Sosseini, asked Khomeini not to make "rash decisions that were governed by his emotions."

The head of the Kurdish Democratic Party, which is the main proponent of autonomy for the region, Abdurahman Qassemlu said: "We were not the ones who started the fight in Paveh. It was started by Revolutionary Guards sent there from another area and the residents of the town were forced to leave when attacked by them."

Meanwhile, the pro-Khomeini Islamic Republican Party newspaper reported that a rocket attack on the U.S. Embassy here early this morning came after a telephone call advised Revolutionary Guards to vacate the compound.

The newspaper indicated the attack may have been carried out by militiamen angry at being removed by the government last week from their post as security guards at the embassy.

An embassy spokesman said the attackers fired two rocket-propelled grenades, one of which failed to detonate and was discovered later.

The spokesman said damage was limited to broken windows and fallen plaster in the embassy's former commissary restaurant, which was being converted into a new consular and visa department. No one was injured in the attack.