Afghan President Babrak Karmal returned from a visit to Moscow to a country braced against possible Moslem guerrilla attacks yesterday, the third anniversary of the Soviet-backed coup that brought him to power.

In Tehran, New Delhi and other capitals, hundreds of protesters marched on Soviet embassies, burned hammer-and-sickle flags and chanted slogans to mark the anniversary.

Western diplomats said extraordinary security precautions had been taken in the Afghan capital of Kabul, with Army patrols on most streets.

In neighboring Pakistan, where nearly 3 million Afghan refugees have fled, extra police were on duty near the Soviet and Afghan embassies in case of demonstrations.

Tehran radio, monitored in London, reported that Afghan protesters staged big marches in Iranian cities. It said a march was held in front of the Soviet Embassy in the Iranian capital but reported no disturbances.

Eyewitnesses reported, however, that police fired warning shots after marchers in Tehran managed to pull down the Soviet flag from the Soviet Embassy.

The official Soviet news agency Tass reported that "riotous elements" had ripped the flag from its staff and tried to force their way into the embassy grounds. The news agency said the Soviet Union filed a "strong protest" over the incident.

Tass yesterday also denounced President Reagan's criticism of the Soviet military presence in Afghanistan and called it a promise to wage "nondeclared warfare" against the pro-Moscow government in Kabul.

The news agency said Reagan "left no doubt that the United States will continue doing its utmost to attain the overthrow of the legitimate government of the sovereign democratic republic of Afghanistan."

Reagan, in a statement issued Sunday, had praised the determination of Afghan "freedom fighters" and said the battle being waged by an estimated 105,000 Soviet occupation troops againt the Moslem rebels was a failure.

China, meanwhile, accused the Soviets yesterday of plotting world domination and of massing troops along the narrow frontier between Afghanistan and China.

The People's Daily, in a front-page editorial on the anniversary of the Soviet intervention in Afghanistan, said Moscow was bogged down in a quagmire and could never defeat Afghan resistance.

"The Soviet invasion of Afghanistan is a major step in the Soviet global strategy for world domination," the official Communist Party paper said.

In protest demonstrations, more than 500 Afghans led by young girls chanting "Down with Russia," marched to the gates of the Soviet Embassy in New Delhi and burned a Soviet flag.

Red paint was splashed over the Soviet Union's consulate building in Geneva and an anonymous caller said it symbolized "the blood of all the Afghans who have been massacred by Soviet troops."

Other demonstrations against the Soviet occupation took place in Paris and Bonn. In the French capital, dozens of protesters carried posters in front of the Soviet Embassy while in Bonn more than 500 persons turned out to support the Afghan people and the Islamic resistance.

Afghanistan's deposed king, Mohammed Zaher Shah, issued a statement in Rome reaffirming "my wholehearted solidarity with the Afghan resistance." He paid tribute to what he called "three years of heroic struggle by an entire nation."