The deposed chief justice emerged from house arrest Monday after Pakistan's new prime minister ordered police to pull back razor-wire barricades and release judges ousted last year by President Pervez Musharraf.

The judge's appearance on the balcony of his Islamabad villa drew cheers from hundreds of flag-waving, drum-beating supporters and dramatically underlined how power is slipping away from a stalwart U.S. ally.

Iftikhar Mohammed Chaudhry and his family had been confined to the house since Musharraf declared a state of emergency in November and sacked 60 senior judges ahead of a Supreme Court ruling that could have invalidated his re-election as president.

"I have no words to thank you for the way you struggled for nearly five months for the enforcement of the rule of law and our constitution," said a beaming Chaudhry as lawyers and opposition activists clapped and threw rose petals.

Just two hours earlier, parliament had elected a loyalist of slain ex-leader Benazir Bhutto as Pakistan's new prime minister following a victory by Bhutto's party in February elections that dealt a crushing defeat to Musharraf's allies.

Yousaf Raza Gilani, a former house speaker who until two years ago was jailed under what he claims were politically motivated charges, beat the pro-Musharraf candidate, Chaudhry Pervez Elahi, by 264 votes to 42.

The new prime minister immediately shook hands with Bhutto's 19-year old son, Bilawal Bhutto Zardari, who watched from the VIP gallery, wiping tears from his face and smiling. His mother held the post of prime minister twice before she was killed in a suicide attack in December.

Cheers of "Long live Bhutto, BB is still alive!" rang out through parliament, as Gilani addressed the house for the first time as premier, saying he would seek a U.N. investigation into Bhutto's killing. He also ordered the immediate release of the detained judges, including Chaudhry.

"Democracy has been revived due to the sacrifice of Benazir Bhutto," he said, as lawmakers thumped their desks in approval.

"We didn't get here out of charity. This moment came because of continued struggle and martyrdom."

Gilani himself spent five years in prison under Musharraf on accusations of abusing his power in making appointments while parliament speaker. A court freed him in 2006.

Gilani, who will be sworn in by the president on Tuesday, will form a government also comprising the party of Nawaz Sharif, who was ousted in Musharraf's 1999 coup.

The coalition partners have vowed to slash the president's sweeping powers and review his counterterrorism policies. Many Pakistanis resent his support of Washington's campaign against al-Qaida and the Taliban, claiming it has stoked a bloody backlash by extremists.

The rapidly changing political climate in Pakistan poses policy problems for the United States.

The Bush administration has been a staunch supporter of Musharraf but in recent weeks has started to put some discreet distance between itself and a once "indispensable" ally in the war on terror.

In Washington, White House spokeswoman Dana Perino congratulated Gilani and the Pakistani people "for moving quickly to form a new government."

The freeing of Chaudhry — who emerged last year as the main check on Musharraf's eight-year domination of Pakistan — is a telling sign of how power is shifting from the military to civilians.

More critical than freeing the judges will be whether the new government honors its promise to reinstate Chaudhry and his colleagues within 30 days — a move that could make Musharraf's position increasingly untenable.

Some believe it could pressure the president to resign even though he has vowed to serve out his five-year term. In November, he gave up his powerful post as army chief.

Musharraf has sounded increasingly bitter about Chaudhry, declaring his reinstatement to be legally impossible. On a recent state visit, he even branded him "scum of the earth."

In his brief address, Chaudhry cautioned that "our destination is still a little far away." He called for continuing support for restoration of the judiciary.

Ayaz Amir, a newspaper columnist and lawmaker for Sharif's party, said there was still some ambiguity in the position of Bhutto's party, but he believed it would be difficult to resist public pressure to bring back Chaudhry.

"They have to take a quick decision or this will be a cloud hanging over the new government," Amir said from the lawn of Chaudhry's two-story hilltop villa.

Earlier, scores of lawyers and opposition activists had converged on the residential enclave for judges, minutes after hearing Gilani's address in parliament. They urged police to roll back the razor-wire barricade that had blocked access to Chaudhry's house since November.

They then ran a few hundred yards up the access road and clambered over the wall of Chaudhry's house because the gates were still locked. Police did nothing to stop them.

"The judiciary was destroyed, but now our people are going to be free and our people will get justice," said Yasir Hussain Shah, a 27-year old lawyer, as jubilant opposition activists danced to drums on the lawn and chanted "Go, Musharraf, Go!"

"No one will have the power to arrest judges again," he said.