At least 21 worshipers, including 7 rabbis, were killed, and 4 others were wounded in the massacre, a blaze of gunfire and explosions that went on for three to five minutes and left the newly refurbished synagogue on fire. The bodies of both gunmen were found in the carnage.

Witnesses described scenes of horror as bullets from automatic weapons raked the benches, worshipers in prayer shawls screamed and fell and blasts shook the Neve Shalom Synagogue, the city's largest, in the Karakoy district near the Galata Tower. One report said the killers also poured gasoline on some victims and tried to burn the bodies.

A 'Horrifying' Scene

''It's horrifying,'' Hasan Ali Ozer, Istanbul's Deputy Governor, said after visiting the scene. Interior Minister Yildirim Akbulut said the killers had barred the synagogue's main doors to keep people from escaping the bullets and grenades. Only 4 of the 29 worshipers escaped unhurt.

Turkish security officials said two Czechoslovak-made submachine guns, seven unexploded Soviet-made hand grenades and more than 100 spent cartridges were found inside the synagogue after firemen put out the flames in the single-story building.

Police officials described the gunmen as Arabs in their 20's and said they were on an apparent suicide mission. A teen-aged survivor, Gabriel Shaun, said they spoke Arabic to one another. Some witnesses said the gunmen entered by posing as photographers or tourists who wanted to take pictures of the interior, but others said they wore masks and dark clothing and simply burst in and began firing. Identification Is Difficult

Bodies and parts of bodies were strewn about the synagogue and many of the dead could not be immediately identified. The bodies were taken away in blood-spattered pine boxes. The four wounded, one seriously hurt, were taken to a hospital.

All the victims were men, most of them elderly, the authorities said. Friends of the victims said that those killed included a visiting Israeli rabbi of Iranian origin, Raphael Nesim, and Rabbi Yuda Adoni, who was leading the morning prayers.

Istanbul, Moslem Turkey's largest city with 5.5 million people, has a Jewish population of about 22,000 Turkish nationals, mostly descendents of the Sephardim who fled the Spanish Inquisition late in the 15th century and still speak an old Spanish dialect known as Ladino. Turkey, Israel, the United States and other countries separately condemned the attack - the first in memory against a synagogue in Istanbul, where the Jewish population has lived in relative peace for years. Many Turkish Jews speak with pride of their long tradition in this country and of the long-standing absence of racial or religious discrimination.

In Tunisia, the Palestine Liberation Organization issued a statement denying involvement and saying it condemned ''the massacre of innocents, even if it concerns Jews.'' Several Claims Reported

Several anti-Israeli terrorist groups said they were responsible for the attack. Anonymous telephone callers in Beirut made claims on behalf of the Moslem fundamentalist group Islamic Holy War, and for a group called Islamic Resistance, a pro-Iranian organization. In Nicosia, Cyprus, a group calling itself the Palestinian Revenge Organization also claimed responsibility.

But the Turkish authorities, while describing the gunmen as Arabs, did not immediately recognize any of these claims as valid. The police cordoned off the area around the synagogue here.

In Ankara, Prime Minister Turgut Ozal, who called the Cabinet into special session, issued a statement condemning the attack. He said it underlined the need for international cooperation to fight terrorism.

'''The occurrence of this heinous act in a place of worship adds to the gravity of the murders and to the indignation felt by the Turkish nation,'' the statement said. 'We Share the Grief'

''All citizens living in Turkey are under the protection of the state, irrespective of their religion, language or race,'' it said, adding, ''We share as a nation the grief and pain of all the families of our fellow citizens who have died because of this odious assault, and express our deepest sympathy to them.''

The Prime Minister's statement also cited the hijacking Friday of a Pan American jetliner at an airport in Karachi that ended with the deaths of 16 people: ''The incident in Pakistan which ended yesterday with so many dead and the criminal attack in our country today clearly show once again the necessity for all countries to work together against international terrorism.''

The Reagan Administration called the attack ''cowardly'' and said that security at ''other key points and installations'' in Istanbul had been heightened. ''We condemn this cowardly attack and deeply deplore the terrible loss of life which resulted from it,'' said Bruce Ammerman, a State Department spokesman.

In Jerusalem, an Israeli Government official called the assault a ''cowardly and criminal act of terrorism.'' 'Place of Peace'