Pakistani President Pervez Musharraf, facing impeachment on charges drawn up by the governing coalition, has announced that he is resigning.
He went on national TV to say that while he was confident the charges would not stand, this was not the time for more confrontation.
He is accused of violation of the constitution and gross misconduct.
Mr Musharraf has been a key ally of the US in its "war on terror" since he took power in a bloodless coup in 1999.
Reaction in Pakistan is overwhelmingly one of relief that a bruising and lengthy impeachment battle has been avoided, the BBC's Mark Dummett reports from Islamabad.
The key issue now is whether the ruling coalition, which had pushed for Mr Musharraf's exit since winning the February election, can stay united and deliver on its promises, he says.
It will have to agree on a new president, then persuade allies like the US and UK, and its neighbours like India and Afghanistan, that it will be committed to defeating militancy and terrorism, our correspondent adds.
International reaction to Mr Musharraf's resignation was mixed, with the US hailing him as strong ally against terrorism but Afghanistan welcoming his departure as a boost to democracy.
Looking calm and dressed soberly in a dark suit and tie, President Musharraf said he had decided to resign after consulting his allies and advisers.
In a defiant speech, he said he had believed it was his destiny to save Pakistan, helped by God, and that he had prevented it from being declared a terrorist state.
In a clear reference to his political opponents, he accused unnamed elements of putting themselves above the country and seeking to betray it.
"Not a single charge can be proved against me," he said, while conceding he had made mistakes.
An impeachment process would have plunged the country into more uncertainty, he said, and it was no time for "individual bravado".
The outgoing president listed social, economic and infrastructural improvements made during his rule.
"I leave myself in the hands of the people," he concluded.
After making his speech, the former military leader inspected a guard of honour outside his white palace in Islamabad, stepped into a black limousine and left the presidency.
Cheering crowds poured into the streets of Pakistan's big cities to celebrate Mr Musharraf's departure. In Karachi, lawyers danced in jubilation.
'A friend to the US'
Once Mr Musharraf's resignation letter is received and accepted by the speaker of Pakistan's lower house of parliament, the speaker of the upper house will take over as acting president.
He is Muhammad Mian Sumroo, a member of the pro-Musharraf faction of the Pakistan Muslim League.
The new president must be elected by both houses of Pakistan's parliament and the four provincial assemblies.
Reacting to news of the resignation, US Secretary of State Condoleezza Rice praised him as a "friend to the United States and one of the world's most committed partners in the war against terrorism and extremism".
She said the US would work with Pakistan's new leaders, pressing on them the need to stem "the growth of extremism".
The UK government wished Mr Musharraf well but stressed that relations did not depend on one individual.
India said it had no comment to make on the resignation since it was an internal matter of Pakistan.
Neighbouring Afghanistan, whose own President, Hamid Karzai, had a very fraught relationship with Mr Musharraf, hoped his departure would boost democracy in both countries.
Mr Musharraf's resignation followed more than a year of turbulence
The unrest began last March when he confronted the judiciary, suspending the chief justice. After widespread strikes and protests, his decision was overturned by the Supreme Court.
Mr Musharraf won the presidential election in October - but the Supreme Court refused to confirm the result.
In November, he declared a state of emergency, citing increasing attacks by militants but eventually stood down as head of the army, giving up his main power base.
The parliamentary election this February, handed a clear victory to the two main opposition parties.
The coalition struck a deal to impeach the president earlier this month and finalised their charges against him hours before he stepped down.