Tehran's worst rioting since the February revolution broke out today when thousands of demonstrators protesting an Iranian government clampdown on the press clashed with stone-throwing Islamic militants loyal to Ayatollah Ruhollah Khomeini.

Hundreds were injured in the rioting, which observers said could portend a revolt by liberal democratic groups and militant leftists against what they see as a new dictatorship of the Moslem clergy.

The rioting was reminiscent of similar outbreaks that eventually led to the overthrow of Shah Mohammad Reza Pahlavi. At the start of the anti-shah campaign in late 1977 and early 1978, demonstrators frequently were set upon by club-wielding thugs organized by the secret police, SAVAK.

Earlier today shooting broke out at the American Embassy compound when the Iranian government moved to replace revolutionary militiamen who have been guarding the mission since February. There were no injuries reported among Americans.

The embassy staff has been complaining about the conduct of the militiamen, who have set themselves up as a quasi-independent force inside the compound - supposedly embassy Feb. 14.

Several of the militiamen were arrested but released later on bail, Tehran Radio announced tonight.

Today's rioting broke out during a march called by the liberal National Democratic Front to protest the government's closure of several newspapers, including the independent daily Ayandegan, and a new press law that imposes stringent restrictions on journalists in Khomeini's Islamic republic.

The march quickly disintegrated into pitched battles as about 5,000 Islamic zealots attacked an estimated 50,000 marchers assembled outside Tehran University.

Shouting pro-Khomeini slogans and denouncing the marchers as Communists, the Islamic militants hurled rocks, bricks and building mortar from dumper trucks that arrived on the scene as part of an apparently organized attempt to disrupt the demonstration.

But the marchers, after briefly retreating into side streets in front of the university, charged back at their attackers and pelted them with the rocks that were littering the streets.

Using their posters and banners as staves and cardboard shields, the demonstrators beat a path for their march to the office of Prime Minister Mehdi Bazargan, where they read a resolution condemning what they called a return to the shah's brand of autocratic rule.

At several points along the route, Islamic militants attacked the marchers with knives, rocks, clubs, chains, bottles and studded belts. In most instances the assaults were repelled and the demonstrators walked on, shouting "Down with dictatorship" and "Ayandegan is the paper of the freedom fighters."

The fiercest fighting erupted outside the prime minister's office after the marchers had read their resolution.

Youths with gashed heads and bloodied faces ran in all directions. Many girls and women who had joined the march were seriously injured.

The organizers of the march, the National Democratic Front, put the toll at about 200 seriously injured, including a member of its Executive Committee. Hospitals reported treating scores of persons for wounds that needed stitches.

The organizers said they had written to the prime minister, the police and the revolutionary authorities informing them of the march and seeking protection.

Only after the two sides had fought for two hours and the marchers had moved back to the university area at nightfall, did security forces appear on the scene. Revolutionary Guards and militiamen fired in the air to disperse crowds milling outside the university. But they permitted a throng of marchers to go by chanting slogans against the government and the ruling clergy, followed at a distance by a band of Islamic militants.

The march and clashes appeared likely to have serious political implications. Already speculation was rife that the Bazargan government might be forced to step down.

Adding to his beleaguered government's problems, both the marchers and their attackers blamed Bazargan for the bloodshed, accusing him of not providing security forces to prevent the clashes.

The National Democratic Front said in a statement after the rioting, "Fascism has truly shown its face today."

While many of the marchers represented the educated middle class from which the Front derives its greatest support for its social democratic policies, a large number were Marxist militants.

They included members of a radical faction of the Marxist Fedaye guerrilla organization that recently went underground. At one stage some marchers shouted, "Islam will be defeated, communism will be victorious."

Witnesses at the U.S. Embassy said the shooting there broke out when Revolutionary Guards acting on government orders tried to disarm a band of militiamen who were originally sent to guard the compound by Khomeini's revolutionary committees.

Since the militiamen installed themselves in the embassy compound, however, they had pilfered the mission's commissary and liquor stocks, started a racket in the sale of cars and household goods left behind by evacuated Americans, and harassed visitors to the embassy.