A close associate of Ayatollah Ruhollah Khomeini was shot and wounded tonight after a second day of anti-American marches that turned into a confrontation between devout Moslems and Iranian leftists.

Ayatollah Hashemi Rafsanjani, a top Khomeini aide and reportedly a member of his secret Revolutionary Council, was hit by two bullets in the stomach outside his home in north Tehran. He was rushed to a nearby hospital where he underwent an emergency operation.

A spokesman for Shohada (formerly Reza Pahlavi) Hospital said the Moslem clergyman suffered damage to his liver but was in satisfactory condition.

No one immediately claimed responsibility for the attack, but there were unconfirmed reports that leaflets dropped near the scene said it was the work of Forqan, a mysterious terrorist group that has claimed responsibility for assassinating two senior figures of the Khomeini government so far.

Rafsanjani, who at about 40 is one of the younger senior clergymen in Khomeini's camp, Thursday had attacked critics of the government as "miserable people" during a speech at an anti-American rally.

Similar demonstrations today were marred by scuffles and fistfights between rival Moslem and leftist protesters.

Islamic Revolutionary Guards stationed at the U.S. Embassy fired several shots into the air to disperse unruly demonstrators at the end of a rally protesting last week's Senate resolution condemning the more than 200 executions ordered so far by Iranian revolutionary courts.

Militiamen also fired their automatic rifles to break up fights at Tehran University, a gathering point for tens of thousands of marchers responding to calls by leftist and liberal groups for a second consecutive day of anti-American demonstrators.

According to hospital sources, at least 25 people were treated for injuries. There were no reports of serious injuries.

The Islamic Republican Party organized marches Thursday to protest the Senate criticism, but also marshaled thousands of demonstrators today to counter the leftist parade. Today's marches, held on a sunny, clear Moslem sabbath, attracted much larger crowds than Thursday's and were more violently anti-American.

The demonstrations were small, however, compared to the hundreds of thousands and sometimes millions, of Iranians who marched in Tehran against the shah during the revolution.

Both Moslem and leftist demonstrators chanted slogans such as, "Death to America" and "Death to Carter." Effigies of President Carter and Israeli Prime Minister Menachem Begin were ripped to shreds by the crowd outside the embassy, and makeshift American flags were burned or torn up to enthusiastic cheers. One effigy of President Carter was suspended from a small wooden gallows.

Throughout the demonstration, protesters told American reporters not to take it personally.

"We are not against the American people, we are against the American government," they said repeatedly.

[In Washington, the State Department deplored the "manifestations of anti-Americanism" and urged "all responsible parties to calm emotions." A spokesman also thanked the Iranian government for protecting the embassy, saying that 100 policemen and militiamen had been assigned to the mission at U.S. request.]

Many of the slogans were directed at the Senate.

"The struggle of the heroic masses is a punch in the mouth for the dirty American Senate," said one slogan spray-painted in Persian on the U.S. Embassy wall.

Several demonstrators interviewed at random singled out Sen. Jacob Javits (R-N.Y.) who introduced the offending resolution, for special denunciation."The Senate of America is Zionist, and he is one of its leaders," a youth said.

As he spoke, loudspeakers wired to trees and mounted on minibuses blared out officially inspired anti-American slogans.

Militiamen assigned to protect the embassy stood atop at eight-foot-high brick wall surrounding the mission's large tree-filled compound. Many of the guards, youths in civillian clothes on loan from local revolutionary committees, cradled automatic rifles with flowers stuck in their barrels.

U.S. Marine guards assigned to the embassy compound were nowhere to be seen.

The demonstration at the embassy was largely peaceful until about 7,000 leftists began marching toward it. Then many of the Islamic demonstrators blocked off the leftists' route and began taunting and harassing them. Thousands of other leftist marchers were immobilized by throngs of Khoemeini followers after leaving the university campus.

"Islam is victorious, communism is defeated," the Moslem demonstrators chanted as the leftists arrived. Most of the Islamic protesters were segregated into groups of men and chador -clad women, while the leftists marched in a long column of both men and women wearing Western clothes.

Leftist marshals at the edges of their parade linked arms as Moslem youths on motorbikes tried to crash into the crowd and assorted scuffles broke out.

"Death to communism," some of the Moslem demonstrators shouted. The leftists changed slogans calling for "unity" and denouncing "imperialism."

The organizers of the Moslem rally helped their followers block the leftists' approach by backing their sound trucks down the street toward the leftists. The leftists then sat down on the asphalt, about 50 yards short of the embassy wall.

Fiercer fistfights erupted when the marchers began to disperse shortly afterward, Khomeini followers tore up the leftists' signs and grabbed their banners. The anti-American aim of both sides seemed to have been forgotten.

The antileftist actions, evidently on the increase, followed a speech by Khomeini Thursday ordering followers to fight those who criticize Iran's Islamic leadership "more vigorously than you did the shah."

Today the Islamic republic's de factor head of state took his campaign against Iranian liberals and leftists one step further, prescribing punishment by revolutionary court for those who insult Moslem clergymen.

In remarks distributed by the official news agency, Khomeini said, "No individual and no group is allowed to insult the clergy, and if it happens the offenders should be prosecuted and punished by the local revolutionary court."

He added, "This warning is made to paralyze the effort of foreign-inspired elements who want to crush the Islamic doctrine." Khomeini called on "the noble Iranian nation" to "denounce those who insult the clergy to the revolutionary court."

The two men killed by Forqan before today's attack on Rafsanjani were Ayatollah Morteza Motahari, 59, a Revolutionary Council member, and former military chief of staff Maj. Gen. Mohammed Vali Gharani.

In mourning ceremonies for Ayatollah Morteza, Rafsanjani delivered a stinging attack on Iranian leftists, who he hinted were responsible for the murder. Other mourning ceremonies were marked by anticommunist demonstrations by Khomeini followers.

In its literature, Forgan, a koranic term meaning either "holy book" or "the difference between truth and falsehood," has used the language of Moslem fanatics but has said it opposes the "dictatorship of the mullahs" in Khomeini's Islamic Republic. CAPTION: Picture, Demonstrators tear up an effigy of President Carter during protests in front of U.S. Embassy in Tehran. AP