KATMANDU, Nepal - The world's last Hindu monarchy is to be swept aside under an agreement between Nepal's former communist rebels and its major political parties that sets the stage for the country once idealized as a Himalayan Shangri La to become a republic.

If it holds, the accord may finally bring a measure of peace and stability that has long eluded this impoverished, near-feudal wonderland for backpackers and mountain climbers looking to scale Mount Everest and other peaks.

At the center of much of Nepal's turmoil has been King Gyanendra, the often-dour and widely reviled head of a dynasty that for centuries held absolute sway over the country - a primacy he sought to reassert nearly two years ago when he dismissed Parliament and seized dictatorial powers.

The power grab was his undoing, and the resulting unrest brought his enemies together, stoked the anger of an already wary public and, as the deal signed this week makes clear, put Nepal on the road to becoming a republic.

"Now there is nothing else that needs to be done," Prachanda, the leader of the former communist rebel, who uses only one name, said yesterday. "There is no monarchy left in the country."

Gyanendra heads a dynasty that dates to 1769, when a regional ruler led an army down from the hills and conquered the ancient city of Katmandu. He established a line of kings that have been traditionally considered reincarnations of the Hindu god Vishnu, to be venerated by their subjects.

In the centuries since, that was often the case. But Gyanendra, the 12th Shah dynasty monarch, has never enjoyed the popularity of his predecessors, and Sunday's deal to eliminate the throne was welcomed by many in Katmandu.

"Before, kings were part of people's heart," said Mata Pasad Risal, 60, a retired government official. "Now people have turned against him. The king has lost his position and popularity. It will be best for him to leave the palace."

From the start, Nepalis were wary of Gyanendra, who before becoming king was known as a hardheaded businessman with interests in tourism, tea, and tobacco.

His tumultuous reign began in 2001 after a palace massacre in which the crown prince is accused of gunning down Gyanendra's older brother, the late King Birendra, and much of the royal family and then killing himself.

In all, 10 members of the royal family were killed, and the slaughter helped pierce the mystique surrounding Nepal's royalty.

Four years later, Gyanendra dismissed Nepal's Parliament and seized total power, saying he would bring order to a chaotic political scene and quell the communist insurgency.

But the insurgency worsened, the economy faltered, and Gyanendra used heavy-handed tactics to silence opposition, jailing and banning critics of himself, his government, and the army.

The result was the communists joining forces with the country's main political parties to orchestrate weeks of unrest in April 2006 that ended with Gyanendra's restoring the parliament. He has since been stripped of his powers, his command over the army, and his immunity from prosecution.

That hasn't been enough for the communists, who are known as the Maoists. They ended their decade-long rebellion - a fight that killed about 13,000 people - last year and later joined the country's interim government.

But they withdrew in September, demanding the monarchy be immediately abolished, a decision the other political parties said could be made only after the election of the special assembly to rewrite the constitution.

The Maoists' move plunged Nepal into a political crisis, derailing plans to elect the assembly and threatening its transition to democracy.

The deal brings the Maoists back into the government by agreeing to eliminate the king once the assembly is elected, a vote officials now say they hope to hold in the first half of 2008.

The king, who said little before ceding power and has said even less since, offered no immediate reaction to the accord.

Many in Katmandu - analysts, officials, and ordinary Nepalis - say they fear he could try to make one last attempt to retain his throne and, perhaps, even regain a degree of his lost power.

But the public is solidly against him, the political parties are lined up to depose him, and the army is thoroughly disillusioned with the abuses soldiers were ordered to commit under his rule. As a result, it is unclear what moves Gyanendra could make, if any, aside from slipping quietly from Nepal's political stage.

Money is said not to be a problem for Gyanendra, who inherited much of his family's wealth after the 2001 massacre. And he has allies among the Hindu nationalists of India, where he was educated.

"If he has any self-respect left in him, he should just walk out of the palace. He has no power, no support, and is facing criticism and anger," said Navaraj Karki, a banker in Katmandu. "It would be better for him and the country if he just disappeared from the scene for good."