Yahya Jammeh, the Gambia’s long term leader has said in an announcement on state TV that he will step down.
Jammeh said it was “not necessary that a single drop of blood be shed”.
The Gambian leader for 22 years had refused to accept defeat in the elections but agreed finally following hours of talks between him and West African mediators.
Adama Barrow who defeated Jammeh in December’s election has been in neighbouring Senegal for days and was inaugurated as president in the Gambian embassy there on Thursday.
Troops from several West African nations, including Senegal, have been deployed in The Gambia, threatening to drive Mr Jammeh out of office if he did not agree to go.
Mr Jammeh’s decision to quit came after talks with the presidents of Guinea and Mauritania.
“I have decided today in good conscience to relinquish the mantle of leadership of this great nation with infinite gratitude to all Gambians,” he said.
“I promise before Allah and the entire nation that all the issues we currently face will be resolved peacefully.”
Mauritanian President shortly before Jammeh’s TV address said that a deal had been struck and that Mr Jammeh would leave the country. He gave no further details.
Mr Jammeh was given an ultimatum to leave office or be forced out by UN-backed troops, which expired at 16:00 GMT on Friday.
The deadline was set by the Economic Community of West African States (Ecowas), a regional grouping backed by the United Nations.
The first signs of a breakthrough came on Friday when a senior aide to the new president told the BBC’s Umaru Fofana that Mr Jammeh had agreed to step down.
Mr Jammeh had at first accepted defeat in the election but then reversed his position and said he would not step down.
He declared a 90-day state of emergency, blaming irregularities in the electoral process.
The electoral commission accepted that some of its early results had contained errors but said they would not have affected Mr Barrow’s win.
Mr Jammeh had vowed to stay in office until new elections were held.