MOSCOW, Aug. 24— The funeral of three young men killed on the streets of Moscow Tuesday night turned into a massive display of solidarity today, as throngs of people filled the center of the city to pay homage to the casualties of this week's failed coup with flags, flowers and pledges to see justice done.

Appearing before a crowd for the first time since his return to Moscow, President Mikhail S. Gorbachev told thousands of people jammed into the giant Manezh Square outside the Kremlin: "Those who participated in the coup will get what they deserve. There will be no mercy for them."

Speaking to an even larger crowd, estimated at nearly 100,000, in the newly named Square of Free Russia outside the building that had been the site of popular resistance to the coup, the Russian President, Boris N. Yeltsin, said the young men had lost their lives in a fight against an enemy that was "cruel and of course, bloodthirsty." He said the coup-makers had prepared a list of 12 people to be killed on the first night of the takeover.

He described the eight men who led the coup as "cockroaches in a jar," squirming as they throw dirt on each other in interviews. Politics and Tradition Call

The political context of the funeral was clear, but many people were drawn by a old Russian tradition of paying last respects with mass demonstrations and the same emotional tug that brought tens of thousands to the funerals of the Nobel Peace Prize winner Andrei D. Sakharov and the poet-balladeer Vladimir Vysotsky, both symbols of opposition to the Communist regime.

"These kids defended us, so why shouldn't we come to pay our debts?" said Polina Makarova, a 50-year-old researcher. "If they hadn't been there, and others like them, then perhaps Moscow today would be in darkness."

Carried live on Soviet television, the funeral procession moved toward the Vagankovskoye Cemetery, where the three men were buried in two ceremonies, one Jewish, the other Russian Orthodox. While the Patriarch of the Russian Orthodox Church, Aleksy II, presided over the elaborate Christian service inside a richly decorated church, the Jewish ceremony was a simple graveside service with a rabbi reading from the Kaddish, the prayer for the dead, over a coffin draped with a Russian flag and a white Jewish prayer shawl, ending with the playing of a solitary violin.

The relatives of the Jewish victim, Ilya Krichevsky, 28 years old, agreed to bury him today with the others, even though Jewish religious law forbids burials on the sabbath. Official Honors for the Dead

The three -- Mr. Krichevsky, Dmitri Komar, and Vladimir Usov -- were killed late Tuesday night when army tanks tried to crash through a barricade of trolley buses on the Garden Ring Road, a central artery. Mr. Krichevsky was shot in the head as he tried to attack a tank, while the other two were crushed under tank treads.

The three were made Heroes of the Soviet Union by Mr. Gorbachev, and their coffins were carried through the streets on the backs of trucks, draped with white, blue and red Russian flags.

Outside the Russian Parliament building, people stood waiting quietly for two hours for the ceremony presided over by Mr. Yeltsin, who stood on a parapet, surrounded by bodyguards protecting him on all sides with bulletproof shields.

Friends and relatives of the victims held up giant photographs and signs praising them for giving their lives in defense of Russia's democracy.

"We will not forget," read one sign. Flags of Russia and other Soviet republics fluttered in a balmy wind.

During the ceremony, people in the crowd passed flowers to the front to be laid along the procession route. One young man began to weep as Mr. Yeltsin bade farewell to the victims, saying,:"Sleep peacefully, and let the earth be soft for you." As the new Russian hymn was played over loudspeakers, people raised their hands with the V-sign, or with clenched fists.

"You have to understand the pain we feel inside," said one middle-aged woman. "We couldn't sleep for three days. If they had won, we would have been destroyed."

Wearing a red armband ringed in black, a solemn Mr. Gorbachev told the crowd at Manezh Square that the young men had died "blocking the way to those who wanted to strangle democracy."

Photos: Thousands marched yesterday in a funeral procession on Kalinin Prospekt, top, toward the Russian parliament in Moscow, to pay homage to the three men killed by troops in the failed coup (Reuters); Muscovites held portraits of the three men -- Vladimir Usov, left, Ilya Krichevski, center, and Dmitri Komar. (Associated Press) Map of Moscow indicating Manezh Square