MOSCOW, JULY 30 -- The Soviet Union and Albania announced the restoration of diplomatic relations today after a 29-year gap, ending a bitter ideological conflict between the world's largest and smallest Communist states.

The decision to resume ties with Moscow marks a major political turnaround for the Stalinist regime in Tirana. Up to a few months ago, it had refused to have anything to do with either the Soviet Union or the United States. Western diplomats here predicted that it could pave the way for a similar opening by Albania toward Washington.

As recently as last spring, Albanian authorities had described Soviet President Mikhail Gorbachev's reform program and the democratic upheaval it spawned in Eastern Europe as a "counterrevolutionary capitalist prescription" and as "unacceptable to our people" and their historic independence from "the foreign powers or their economic, military and political blocs."

But a change of tone began creeping into Albanian statements over the last few months, with Tirana hinting that it could be ready to establish ties with both Moscow and Washington. Today's joint announcement was carried by the official Soviet news agency Tass and the Albanian news agency ATA and followed two rounds of talks in Sofia, the Bulgarian capital, in late June and today in Tirana, the Albanian capital.

"The Soviet Union and Albania have agreed to normalize their relations and reopen embassies in Moscow and Tirana," the official announcement said, adding that the two nations looked forward to an expansion of contacts in "political, economic, scientific, technical, cultural and other areas."

As the world's first socialist state, the Soviet Union acted as an economic and ideological patron to the Communist regime that came to power in Albania as the result of a guerrilla uprising against occupying Italian and German forces in World War II. But relations between the two countries soured in the mid-1950s when Soviet leader Nikita Khrushchev began questioning the legacy of Joseph Stalin, who was held in great esteem in Albania.

A fervent disciple of Stalin, Albanian Communist state founder Enver Hoxha suspected that Khrushchev had strategic designs on his Balkan country of 3.2 million people sandwiched between Yugoslavia and Greece. In his memoirs, Hoxha describes how he indignantly rejected a proposal by Khrushchev to open a Soviet naval base at the Albanian port of Shkoder on the Adriatic Sea, telling the Soviet leader to take his idea and "stuff it in your pocket."

The final rift came in the early 1960s when Hoxha sided with China in the rapidly escalating Sino-Soviet dispute. At first, Soviet ideologists were reluctant to criticize China directly and concentrated their fire instead on "Albania and its ally." On Dec. 9, 1961, the Soviet Union broke off diplomatic relations with Tirana.

Albania responded by withdrawing from the Soviet-led Warsaw Pact military alliance and from Comecon, the East European trading organization. Under Khrushchev's successors, the Soviet Union attempted several times to heal the rift with Albania, particularly after Hoxha quarreled with China in 1977. But Moscow's initiatives were always indignantly rejected by Hoxha, who insisted until his death in 1985 that Albania was the only genuine Communist country in the world and would never compromise its ideological principles.

In 1982, the Albanian Communist Party newspaper ruled out resumption of diplomatic relations with the "revisionist Soviet Union" for all time, saying that the Kremlin was following an "imperialist policy aimed at enslaving the world, at suppressing and exploiting peoples."

The Albanian policy of defiant isolation became more difficult to sustain following Hoxha's death and the cessation of Chinese economic assistance in the same year. Albania became even more of a political odd man out last year after hard-line Communist regimes were swept from power throughout Eastern Europe.

The pressures on Hoxha's successor, Ramiz Alia, increased this summer as thousands of Albanians seeking escape to the West took refuge in foreign embassies in Tirana and were ultimately allowed to leave the country. The new Albanian leader is now attempting to open his country gradually to the outside world, apparently hoping at the same time to preserve its political options by using the Soviet Union as a counterweight to Western Europe and the United States.

ATA said that the agreement had been negotiated by Soviet Deputy Foreign Minister Yuli Kvitsinsky, best known in the West as the Soviet diplomat who took a "walk in the woods" with U.S. arms negotiator Paul Nitze in 1982 during talks in Geneva on reducing intermediate-range nuclear missiles. It said Kvitsinsky signed the agreement today in Tirana with Albanian Deputy Foreign Minister Sokrat Plaka.