King Hassan II of Morocco, in a deft stroke of diplomacy, has bought more time in his war over the disputed Western Sahara with a peace initiative that leaves his desert troops in recently improved positions while blunting attacks by backers of his guerrilla adversaries.

The initiative, put forward at last month's meeting of the Organization of African Unity in Nairobi, centers on his offer for a cease-fire to be followed by a vaguely defined "controlled referendum," which U.S. and other analysts expect to generate months of argument over who would vote, where and under whose supervision and control.

Already, Saharan guerrillas are demanding Moroccan evacuation as a condition for any referendum and experts are disputing the validity of the only official census as a way to determine the electorate. In the meantime, U.S. and European diplomatic sources point out, Moroccan forces have in the last few months scored defensive gains with unexpected help from a 10-foot-high sand embankment, judged by U.S. military specialists to be "at lest temporarily effective" as a barrier against hit-and-run attacks by the Polisario guerrillas fighting for an independent Saharan Arab Democratic Republic.

Hassan, by reversing earlier refusals, has nevertheless now complied in principle with the OAU's longstanding demand for a referendum to determine who rules the Western Sahara, a mostly barren territory that was a Spanish colony until 1975. After Spanish withdrawal, it was divided between Morocco and Mauritania. But Mauritania, shaken by guerrilla pressure, religquished its claim two years ago and Hassan now is fighting to control the entire Colorado-sized expanse of sand.

His agreement to hold the referendum -- however ill defined -- has thus saddled his Polisario foes with the international blame for any continued fighting and burnished his own image at home and in OAU councils, the diplomatic sources say. But at the same time, they add, he has retained the ability while the referendum talks drag on to further entrench his defensive positions in the section of the Western Sahara sealed off by the sand barricade and christened the "useful Sahara."

"Hassan has been able, as he has in the past, to obtain one of his major objectives -- and that is to buy some more time," a U.S. official said. "He's won a diplomatic victory . . . in a rather brilliant series of maneuvers.

A measure of the victory was the bitterness of Polisario reaction when Hassan's offer was accepted by the OAU on June 26. Polisario representatives, who had been hoping that more African countries would recognize their self-declared Saharan government during the Nairobi gathering, dismissed Hassan's offer as a maneuver and criticized OAU members for selling them out.

Particularly galling was silence from Libya.

The Libyan leader, Col. Muammar Qaddafi, has been a financier for Polisario and one of its loudest if not most effective supporters. In the days preceding the OAU meeting, however, Qaddafi and Hassan ahd sent low-ranking and little-noticed envoys to each other's capitals and quietly announced their intention to resume diplomatic relations. While no mention was made of the Sahara at the time, the first conspicuous result was Libyan reserve in Nairobi.

U.S. sources, noticing the shift, speculate that Qaddafi is concentrating on "the old Arab unity routine," a familiar if often feckless theme for the Libyan leader that in this case plays into Hassan's hands.

One reason, they say, could be fear by Qaddafi that Israel might attack his nuclear research facilities as it attacked the Iraqi reactor near Baghdad. Another, they add, could involve a trade-off whereby Qaddafi turns a blind eye toward Moroccan actions in the Sahara while Hassan applies similar discretion to Qaddafi's disputed intervention in Chad.

The Polisario's other main backer, Algeria, was caught off-guard by Hassan's proposal and as a result also held its tongue at Nairobi, U.S. analysts say. Its position is not expected to be made clear until next month at the earliest, when a seven-nation OAU committee is scheduled to meet to begin organizing the referendum.

In the meantime, the analysts say, there have been no signs of diminished Algerian or Libyan military support for the guerrillas.

Hassan's troops in the Western Sahara, about 80,000 strong from a 140,000-man military, nevertheless have in recent months reduced the Polisario's ability to strike at targets in the "useful Sahara" comprising the main towns of Al Ayun and Samara as well as phosphate mines at Bu Craa.

This is in part due to stepped-up Moroccan air strikes against Polisario jeep columns, reducing the mobility vital to Polisario's some 10,000 irregulars. But U.S. military specialists say the wall, called a "berm" by army engineers, also has played a key role, despite earlier predictions that it would turn out to be a desert folly.

The barrier, which stretches in a 250-mile protective arc around the "useful Sahara," is for most of its length a reinforced sandbank rising behind a ditch, with strong points every few miles and backup forces to react swiftly to any attack. In addition to keeping Polisario guerrillas out, the specialists say, the wall also makes their getaway more difficult if they do infiltrate.

Although the war for the Sahara is thought by most specialists to have broad support within Morocco, Hassan was in bad need of some visible signs that the end is approaching, diplomatic sources say. The fighting has taken its toll on the kingdom's finances -- 40 percent of the national budget is devoted to the military -- and riots June 20 in Casablanca underscored the depth of dissatisfacton caused by economic disruptions intensified by a prolonged drought.

According to an official count, more than 60 persons were killed and more than 100 injured when the disorder erupted over price rises for staple foods that in some cases amounted to several hundred percent. Reports from the city said rioters directed their ire particularly at banks. About 2,000 persons are on trial for charges arising from the riots, described as the worst since the 1960s.