Gambia's President Yahya Jammeh, who for weeks had refused to step down after losing the recent election, has confirmed that he has decided to relinquish power.

"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 in a statement broadcast on state television early on Saturday.

The announcement came after hours of last-ditch talks with regional leaders and the threat by a regional military force to make him leave.

The new president, Adama Barrow, had earlier declared that "the rule of fear" was over and that Jammeh, who ruled the country for 22 years, had agreed to leave.

I would like to inform you that Yahya Jammeh has agreed to step down. He is scheduled to depart Gambia today. #NewGambia — Adama Barrow (@adama_barrow) January 20, 2017

Barrow was sworn in at Gambia's embassy in Dakar in neighbouring Senegal on Thursday.

A State House official close to the situation told the AFP news agency that Jammeh would leave within three days, possibly on Saturday with Alpha Conde, president of Guinea, who had travelled to Gambia's capital, Banjul, for negotiations.

Earlier on Friday, Gambia's Chief of Defence Forces, Ousmane Badjie, pledged his allegiance to the country's new president, a major shift as mediation continued to persuade Jammeh to cede power.

Jammeh had rejected Barrow's December 1 election win, despite significant pressure from regional powers and the UN, sparking a major crisis.

READ MORE: Is Gambia on a path to turmoil?

At least 46,000 people have fled Gambia for Senegal since the start of the crisis fearing unrest, the UN's refugee agency UNHCR said, citing Senegalese government figures.

Celebrations erupted in Banjul, where tensions have run high especially since the declaration of a state of emergency by Jammeh made on Tuesday.

Barrow, a real estate agent turned politician, had flown into Senegal on January 15 to seek shelter after weeks of rising tension over Jammeh's stance.

West African troops had entered the country to bolster the new president, but military operations were suspended in favour of a final diplomatic push to convince Jammeh to exit peacefully.

In an interview with Al Jazeera, Barrow said that he hoped the 15-nation Economic Community of West African States (ECOWAS) could find him a safe haven.

Jammeh started negotiations with ECOWAS on Thursday. He demanded an amnesty for any crimes that he may have committed during his more than two decades in power and that he be permitted to stay in Gambia, at his home village of Kanilai.

Those demands were not acceptable, said Marcel Alain de Souza, head of ECOWAS.

Jammeh's continued presence in Gambia would "create disturbances to public order and terrorist movements", he said.

Al Jazeera's Nicolas Haque, reporting from Dakar, said: "A stable Gambia, Barrow told me, has to be without Jammeh in the picture. That's why this news is quite significant for all those that have left the country - 46,000 since January 1 ... They hope he leaves so they can come back."