MOSCOW -- As the last few thousand Soviet soldiers leave Afghanistan, they are returning to a warm welcome from a relieved nation happy to have them home, but also to a growing political controversy over the nine-year war.

"Our boys are coming home," a Soviet television commentator said over the weekend, summing up the universal satisfaction both at the troops' return and at the knowledge that by Wednesday, the country will finally be out of an unwinnable and increasingly unpopular war.

"Hello, Motherland!" the government newspaper Izvestia said in a headline over one of its front-page reports on the withdrawal last week. "We've Waited for This for Nine Years," another front-page headline declared.

Each evening, Soviet Television shows more tanks, armored personnel carriers and trucks full of troops crossing the border from Afghanistan into Soviet Central Asia. Their clear joy is infectious as the cameras focus on the long-awaited family reunions and then on the on-the-spot demobilization of many soldiers.

And, like his troops, Lt. Gen. Boris Gromov--the Soviet commander in Afghanistan--will cross Friendship Bridge linking the Soviet town of Termez with Hayratan on the Afghan side "without looking back," a correspondent for the Soviet youth newspaper Komsomolskaya Pravda reported.

Expected to be the last Soviet soldier to leave Afghanistan on Wednesday, the deadline for completion of the withdrawal of the 100,300 troops who were there when the pullout began nine months ago, Gromov will pause only for a minute or so to reflect on the long war, the paper said, and then he, too, "will have returned to the warm embrace of the motherland."

Although there are few emotions deeper than the joy of a homecoming for a Russian, the Soviet troops are returning to a growing and increasingly bitter political debate over whose mistake it was to invade Afghanistan in late 1979 and then to stay nine years, fighting on behalf of a government now perceived as largely without popular support.

One of "our old sins," President Mikhail S. Gorbachev called it during a speech here last month--only to change the characterization two days later, when his remarks were published, to a criticism of the "considerable expenditures" involved in the war.

Nearly 15,000 Killed

With nearly 15,000 Soviet personnel reported killed, huge resources wasted and the country's international prestige badly damaged in the war, this political sensitivity stems first from the question of responsibility.

The late President Leonid I. Brezhnev is officially being blamed for this and most of the country's other political, economic and social ills. But with its principle of collective leadership, the Communist Party, which repeatedly endorsed the Afghanistan action as in the finest traditions of the international Communist movement, is being pressed now to acknowledge the war fully and formally as a fundamental error.

As party members seek election next month to the new Congress of People's Deputies, Afghanistan is an issue on which they are frequently challenged as they assert the correctness of perestroika , the political, economic and social restructuring begun by Gorbachev in 1984.

"Why has it taken nearly four years under Gorbachev, under perestroika , to complete our withdrawal?" a trade union delegate demanded of a party official during a nomination meeting here last week. "Even now, with almost all of our boys back home, why can't you say we were wrong to go in? Why do you still say it was our 'internationalist duty' to defend a government and a party that were never socialist and came to power through a coup d'etat ?"

Cracks Are Showing

The party is still grappling with such questions itself, and, amid the continuing political ferment here, the divisions are increasingly apparent.

"Unfortunately, it has taken nearly 10 years," Alexander Bovin, a leading Soviet commentator, said recently of the decision to withdraw. "It took thousands of deaths, enormous materials costs, a fall in our country's prestige and the exacerbation of the international atmosphere to understand--to understand not in theory, but in practice--that every people has to decide its own fate itself, without outside interference.

"This conclusion is drawn not because we proved powerless to change the natural course of events in another country. This conclusion is drawn because the very attempt to change that course of events was mistaken in principle."

Bovin, whose sharp criticism has been cut several times from television commentaries, acknowledged that many, particularly those who fought in Afghanistan, find it difficult to accept this view, which contradicts past ideals of "internationalist duty" and "fraternal assistance."

Bitter Truth