MOSCOW, SEPT. 17 -- Soviet President Mikhail Gorbachev and Saudi Foreign Minister Prince Saud Faisal reestablished diplomatic relations between the two countries today after a rift of 52 years and agreed to work together to try to persuade Iraq to withdraw its forces from Kuwait.

According to the Soviet news agency Tass, the two parties issued a joint statement that read in part: "We hope that faced with the world community's unanimity, Iraq will agree to the immediate and unconditional withdrawal of its troops from Kuwait and a restoration of legality."

The strongly anti-communist regime in Saudi Arabia has inched steadily closer to Moscow in recent years as a means of balancing its ties with the United States, a staunch ally of Israel, but it was the current crisis in the Persian Gulf that made today's agreement possible.

Moscow's willingness to join the West in condemning Iraq's invasion of Kuwait despite Moscow's 30-year friendship with the Baghdad government apparently reshaped opinion in the Saudi hierarchy.

Saud told a Saudi newspaper Sunday: "In view of the positive role played by the Soviet Union in ensuring security and stability in the world in general and the Middle East in particular, I believe the time is now more opportune than ever to set up active and effective ties between the two countries."

Faced with an inherent conflict between a conservative Islamic regime and a totalitarian, atheistic regime that destroyed churches and mosques, the two countries broke off ties in 1938. Among other reasons for the break was Soviet dictator Joseph Stalin's refusal to allow Soviet Moslems to make pilgrimages to Mecca. In later years, the Saudis financed a number of anti-communist movements on ideological grounds.

At his press conference with President Bush in Helsinki this month, Gorbachev said that the Soviet Union is prepared to play a more active role in Middle East diplomacy. Foreign Minister Eduard Shevardnadze has also offered to visit Baghdad to help foster a peaceful settlement of the gulf crisis and has called for a series of multinational conferences on the Middle East that would include sessions on the Iraqi invasion, the Israeli-occupied territories and the Lebanese civil war.

Gorbachev and Saud discussed the gulf crisis, Tass said, "and assessed the possible conduct of the aggressor and those opposing it. They also discussed ways to achieve goals set in U.N. resolutions" imposing sanctions against Iraq.

Tass said both Saudi Arabia and the Soviet Union felt their lack of economic ties was "unnatural." Relations between the two countries improved considerably after Soviet troops began withdrawing from Afghanistan, a Moslem country, in 1988.

In recent years, the Soviet Union has become increasingly concerned about the political direction of its 50 million Moslems, who live mainly in Azerbaijan and Central Asia. Religious consciousness has become an important element of nationalism in those regions.

As a result, Soviet officials have begun allowing more Moslems to visit the Islamic holy places in Saudi Arabia. This year, about 1,500 made the trip, in contrast to recent pre-Gorbachev years, when only a dozen or so were given permission.

This week, the Saudi minister of pilgrimages arrived here for a three-day conference with Soviet Moslems.