The Soviet Union's release of Anatoly Scharansky to the West resulted from a senior-level reassessment of the political costs of keeping him imprisoned, in the view of western diplomats here.

Kremlin officials agreed to set Scharansky free primarily to remove an impediment in improved relations with the West, according to U.S., West German and other western officials in the Soviet capital.

In so doing, they said, the new Soviet leadership under Mikhail Gorbachev reversed the official treatment of the case of the mathematician and Jewish human rights activist in recent years, they said. Past Soviet governments have rejected chances to parole Scharansky, set very high terms for his release and extended his prison sentence.

Western diplomats, Soviets who have been denied permission to emigrate (called "refusedniks") and longtime friends in the Soviet capital cheered Scharansky's discharge. But they consider it Moscow's way of relenting to heavy lobbying from U.S. and Western European leaders in a specific case, rather than a humanitarian gesture, or a signal of an easing in Soviet policy on human rights.

"At a time in which the Kremlin is seeking improvements in U.S.-Soviet relations," said one western diplomat who knew Scharansky, "letting him go became more useful for them than keeping him."

Scharansky, 38, two westerners and a Czechoslovak were set free today in Berlin. In exchange, a Soviet and four East Europeans held in the West for spying were released.

Soviet officials, questioned about Scharansky's release in a Foreign Ministry press conference today, declined comment. The event was not reported in the Soviet media.

Obtaining the Soviet and East European spies imprisoned in the West was not a decisive factor in today's prisoner exchange, although it encouraged Moscow to proceed with the deal, the diplomats said.

More important than receiving Soviet Bloc spies was the opportunity the so-called spy swap gave for the Soviet Union to continue its insistence that Scharansky was a spy, one western diplomat said.

Scharansky was arrested in 1977 and tried and imprisoned by a Moscow court in 1978 on charges of spying for the CIA.

Then-president Jimmy Carter and other U.S. officials have denied the charges.

Subsequent U.S.-Soviet negotiations for Scharansky's release ended in a quagmire, western diplomats here said.

At least twice, proposed exchanges broke down because the Soviet Union's demands were too steep, a senior western diplomat said.

In recent months, U.S. and French officials raised demands for Scharansky's and dissident physicist Andrei Sakharov's release directly with Gorbachev. The demands were made during Gorbachev's October 1985 visit to Paris, Secretary of State George P. Shultz's visit to Moscow in November 1985 and the summit meeting between Gorbachev and Reagan later that month in Geneva, according to western diplomats here.

Scharansky's wife Avital, who emigrated from the Soviet Union in 1974, rallied French and American support for her husband over the years.

Members of the Moscow refusednik community, which had been in the forefront of the effort to win Scharansky's freedom, greeted his release to the West and reunion with his wife warmly. But they expressed skepticism that the event signals a general improvement in the human rights situaton.

"We are happy for Anatoly," said one activist, raising a glass of red wine. But, she said of the Soviet leadership, "they will not give too much. Maybe they will think this one case is enough for now."

In an interview in the French Communist Party newspaper L'Humanite last week, Gorbachev said that Sakharov, exiled in the closed Soviet city of Gorki, would not be allowed to leave for the West.

Since 1979, when Jewish emigration to the West peaked at 51,000, the level has fallen dramatically. Last year's figure of 1,140 reflected a minimal increase over the 1984 level.

There are tens of thousands of refusedniks in the Soviet Union, and about 2 million Jews, according to western estimates.

Two longstanding cases have received particular support in the Moscow human rights movement:

*Yuri Orlov, a physicist and leader of the Helsinki Watch human rights monitoring group who was tried and exiled to a labor camp in 1978, the same year in which Scharansky was imprisoned.

*Josef Begun, a Jewish human rights activist who was tried for anti-Soviet agitation and propaganda in 1982 and sentenced to seven years in a labor camp and five years' exile.