Thousands of Afghans and their Iranian supporters stormed the Soviet Embassy here today and ripped down the hammer-and-sickle flag before Iranian police, firing dozens of shots into the air, drove them away.

The demonstration was the most violent in Tehran since the seizure of the American Embassy by militant Moslems eight weeks ago. It coincided with the arrival here of U. N. Secretary General Kurt Waldheim on a mission to gain release of the American hostages, estimated by U.S. officials to number 50, held at the U.S. Embassy.

Waldheim, who is expected to begin official talks with the Revolutionary Council Wednesday, said at the airport on arrival that his visit is designed to "exchange views . . . and to pave the way for a solution to the crisis." Militants holding the embassy have said they would not meet with Waldheim unless ordered to do so by Islamic revolutionary leader Ayatollah Ruhollah Khomeini.

The attack on the Soviet Embassy, just 10 blocks from the occupied American Embassy, was one of two major protests in Iran today against the soviet-led coup in neighboring Afghanistan last week.

In Mashad, near the Afghan border, about 5,000 Iranians and Afghans demonstrated outside the Soviet consulate, burning a Soviet flag and painting slogans on the walls. No injuries or arrests were reported.

In the demonstration here, thousands of protesters, chanting,"Americans and Soviets are enemies of our people," marched on the Soviet embassy.

Several scaled the fence surrounding the compound, which is about the same size as the 24-acre U.S. Embassy grounds. They tore the Soviet flag into pieces with their teeth and hands and set the fragments on fire.

The angry crowd was finally repulsed by Iranian police guards firing into the air and locking their arms at the embassy gates, but not before the red Soviet flag was replaced with a white-and-green Islamic banner bearing the slogan, "There is no god but Allah."

Let by turbaned Moslem mullahs from Afghanistan, the highly charged protesters left the Soviet compound and marched to the American Embassy, alternating denunciations of the two superpowers. "Death to Russians," they shouted, "Death to Communists . . . down with Brezhnev . . . death to Carter, death to imperialists."

Tass, the official Soviet news agency, in a brief report on the demonstration, called the protesters "hooligans" and said they were "believed to include persons hostile to the Afghan revolution who fled from Afghanistan."

Iran has officially protested the Soviet involvement in Afghanistan. The Foreign Ministry said in a statement last week, "We strongly condemn the military intervention of the Soviet Union in Afghanistan and hope the Soviet Union will immediately withdraw its forces from Afghan territory, if only as a gesture to prove its support for the anti-imperialist struggle of Iran."

The militant Moslem students holding the U.S. Embassy distanced themselves from the protest at the Soviet Embassy, saying, in a broadcast statement, that "although we look forward to the day when the banner of Islam is hoisted over the embassy, nevertheless, we condemn this unprincipled act 100 percent."

Meanwhle, Waldheim began his efforts to free the Americans held by Islamic students in the occupied U.S. Embassy for the past eight weeks. The U.N. Security Council voted 11 to 0 with four abstentions on Monday to approve a U.S. sponsored resolution against Iran if Waldheim fails to obtain the release of the American hostages in Tehran by Jan. 7.

Speaking at Mehrabad Airport after his jet landed, Waldheim, who is the highest ranking diplomatic figure to enter the hostage negotiations, held out little hope that his mission would succeed before the seven-day deadline set by the U.N. Security Council.

"You cannot expect from such a first visit to solve immediately all problems," said Waldheim, who planned this trip before the Security Council passed its resolution.

"That is not being realistic," he said.

Waldheim is expected to encounter difficult obstacles in his attempts to free the hostages, a mission seen as doomed from the start by Iranian leaders who have expressed disappointment with recent diplomatic gestures to the United States and appear in no mood to negotiate with a middleman.

Even before he left New York, the militant students holding the Americans belittled Waldheim's efforts. In an Iranian newspaper interview today, a student spokesman reaffirmed that position, calling the mission "a deceptive game . . . [that] will not solve anything and will not be of any benefit to our people."

Claiming that on his last visit to Iran when the Islamic revolution was gaining steam, Waldheim had ignored that "the nation faced with the shah's machine-gun fire was crying in blood," the spokesman said, "he will not bring us any presents this time either."

Ayatollah Ruhollah Khomeini, Iran's Islamic leader who possesses the only control over the students, has made no plans to see the U.N. envoy and his rebuffs of previous overtures by such influential figures as the pope show that he is not swayed by such high-level appeals.

Although Khomeini has not publicly referred to Waldheim's visit, the two Iranian newspapers that most consistently reflect the 79-year-old divine's thinking published old photographs in today's editions showed Waldheim kissing the hand of Ashraf Pahlavi, twin sister of the hated deposed shah.

Making the diplomatic climate even chillier for the secretary general, Iran's national television station broadcast a documentary tonight juxtaposing pictures of the victims of last year's Islamic revolution next to shots of a smiling Waldheim getting into a Mercedes for a meeting.

Foreign Minister Sadegh Ghotbzadeh, who has the unique position of dealing both with Khomeini and the students, met Waldheim at the airport today, but made no remarks. At a press conference earlier, he stressed that Waldheim was here to gather information rather than negotiate the release of American hostages.

U.N. secretary general told reporters, "My visit to Iran will offer a welcome opportunity for me to have an exchange of views with the Iranian authorities and I hope this will contribute to fine means and ways to solve the crisis."