ISLAMABAD (Reuters) - Pakistan's government agreed on Monday to reinstate Iftikhar Chaudhry as chief justice in a surprise move to defuse a crisis and end agitation by lawyers and activists that had threatened to turn into violent confrontation.

Chaudhry's reinstatement will cool tension but friction is likely to persist between President Asif Ali Zardari, who analysts say has been weakened by the controversy over the judge, and his emboldened rival, former prime minister Nawaz Sharif.

It's the first time in the history of Pakistan that a movement launched by the middle class has proved successful, said retired judge Tariq Mehmud, a leader of the lawyers.

Chaudhry became a cause celebre after being dismissed in late 2007 by then-president and army chief General Pervez Musharraf, apparently because Musharraf feared the judge would challenge his constitutionally questionable strategy to hold on to power.

Opposition leader Sharif had thrown his support behind the anti-government lawyers' campaign that was bringing added turmoil to nuclear-armed Pakistan, where the government has already been struggling to stem militancy and revive a flagging economy.

Prime Minister Yousaf Raza Gilani made the announcement in a televised address to the nation. Afterwards, Sharif called off a long march protest making its way to the capital, Islamabad.

The crisis gripping the Muslim nation had alarmed the United States and Britain, which fear any slide into chaos would help the Taliban and al Qaeda become stronger in Pakistan.

The United States welcomed Chaudhry's reinstatement.

This is a statesmanlike decision taken to defuse a serious confrontation, and the apparent removal of this long-standing national issue is a substantial step toward national reconciliation, the U.S. embassy said.

The main stock index, which has been hurt by political worry, ended 5.4 percent higher. It had fallen 1.9 percent this year after a 58.3 percent slide last year.

Some analysts saw Chaudhry's comeback adding to Pakistan's complexities.

The reinstatement ... will further complicate politics, said Brian Cloughley, a British defense analyst familiar with Pakistan. Nobody knows what his allegiance is, in terms of Pakistan's constitution.

Zardari, elected by parliament six months ago, had feared Chaudhry could wage a vendetta against Musharraf, which could also threaten Zardari's own position.

Zardari yielded after Sharif and the lawyers held a day of protest in Lahore and set off for Islamabad for a sit-in outside parliament. Authorities had put shipping containers and trucks on roads to stop the protesters entering the capital.

Chaudhry will be reinstated on March 21 when the incumbent retires.

CHASTENED

Some analysts saw the move as helping stability, although divisive issues remained between Zardari and Sharif.

There will be greater faith in the political order and in that sense, it will contribute to stability, said former government minister and analyst Shafqat Mahmood.

The judge has a reputation for ruffling feathers but would be prudent, analysts said. All the players are a bit chastened with what has happened over the past two years and I don't foresee any precipitate adventurism, Mahmood said.

Jubilant lawyers gathered outside Chaudhry's residence. He briefly stepped outside to greet and thank supporters and wave to the cameras.

Western diplomats had tried to make Zardari pull out of a collision that could have destabilized the year-old coalition and forced the military to intervene. Army chief General Ashfaq Kayani was involved in negotiations that led to the judge's restoration.

Sharif, a two-time prime minister with conservative, religious-nationalist support, was overthrown by Musharraf in 1999. Since returning from exile in 2007 he has become Pakistan's most popular politician, thanks partly to his stand on the judge.

Sharif was conciliatory, congratulating Zardari and Gilani.

We have got the fruit of our two-year struggle, Sharif told supporters. Authorities later released hundreds of activists who had been rounded up in a crackdown aimed at stifling the protest.

Zardari, widower of assassinated former prime minister Benazir Bhutto, was elected by parliament last September after forcing Musharraf to quit the presidency.

Sharif latched onto Chaudhry's cause two years ago, but the latest crisis began when Zardari ejected Sharif's party from power in Punjab last month, after the Supreme Court barred Sharif and his brother from holding elected office.

In a first step toward reconciliation, the government said on Saturday it would seek a review of that court ruling.

(Additional reporting by Zeeshan Haider and Robert Birsel; Writing by Simon Cameron-Moore; Editing by Paul Tait)