The Soviet news media found a diplomatic victory in the retreat from Afghanistan, portraying the Soviet pullout as a triumphant end to eight years of aid to a neighbor in need.

The state-run press, while conceding that military victory proved elusive, nevertheless put a positive spin on the withdrawal that began Sunday."Homeward!" the Communist Party daily Pravda declared on its front page Monday, showing a smiling Afghan officer chatting with a Soviet major just before the major's departure for home.

"We were heading for this for a long time and with difficulty," Pravda wrote. "But life, with its imperious hand, moves aside the most sincere strivings and spoils the best-laid plans."

Pravda depicted the withdrawal as a diplomatic triumph over countries that supported the anti-communist guerrillas in their battle against President Najib's Moscow-backed government.

The party daily said that the April 14 Geneva accord that set the timetable for withdrawal was the result of opposition forces finally realizing that the war could not be won by military force alone.

"We never laid claim to a military victory in Afghanistan," Pravda said, adding: "If we speak of defeats and victories, then there is a victory, too . . . The return of our sons is a victory for perestroika."

Perestroika refers to Soviet leader Mikhail S. Gorba-chev's reform program aimed at rebuilding society.

Television news coverage, such as reports by the nightly news program Vremya, have focused on the ceremonial send-offs given the Soviet soldiers by the Afghan government.

Afghan soldiers and citizens were lined up on the streets of Jalalabad when the Soviets pulled out of their base there Sunday and in Kabul, where about 1,300 troops marched out on Monday, the first contingent to leave the country under the Geneva accord.

But while the ceremonies took place, gunfire could be heard in the distance, a reminder that the war continues. Guerrilla factions have vowed to continue their fight to topple the Marxist government.