Sri Lanka's president on Sunday reappointed as prime minister the same man he sacked from the job nearly two months ago, ending a messy power struggle that had paralysed the island nation.
Ranil Wickremesinghe, whose shock dismissal in late October threw Sri Lanka into constitutional crisis, was sworn in at a closed-door ceremony in the president's office in Colombo.
The 69-year-old had refused to step aside since being dumped and replaced by controversial strongman Mahinda Rajapakse - leaving the country with two rival leaders and no functioning government.
The ousted premier had long asserted his dismissal was illegal, a view supported by Sri Lanka's parliament which six times voted against Rajapakse's claim to rule during tumultuous sessions that erupted into brawls.
President Maithripala Sirisena had refused to bow to pressure as the country drifted, declaring he would never reappoint Wickremesinghe and deriding his once-ally in public speeches as their alliance imploded.
The acrimony between the two was underscored Sunday when Sirisena barred journalists from attending the swearing-in ceremony - leaving it to Wickremesinghe's legislators to announce the appointment.
"We thank the citizens of the country who fought the illegal seizure of power and ensured that democracy was restored," his United National Party of Sri Lanka posted on Twitter.
A photo of the foes smiling and bowing at each other circulated widely on social media, and crackers were burst in celebration in parts of the country.
A spokesman for Wickremesinghe said he was expected to form a cabinet in the coming days with priority given to the 2019 budget - without which Sri Lanka risks government shutdown and defaulting on its sizeable foreign debt.
On Wednesday, the legislature voted overwhelmingly to demand the reinstatement of Wickremesinghe.
Sirisena's resistance became untenable after the country's highest court last week ruled that he acted outside the constitution when he dissolved parliament in early November and called early elections.
The Supreme Court also confirmed that Rajapakse could not exercise the powers of a prime minister until he proved his legitimacy - which without enough support in parliament was impossible.
Sirisena's disputed appointee had pressed ahead, forming a purported government and naming a cabinet even as parliament cut off state funds to his office.
But on Saturday Rajapakse - who presided over the bloody end to Sri Lanka's civil war in 2009 - backed down from the post of prime minister, signalling the 51-day standoff had come to a bitter close.
The strongman, whose past administrations were accused of gross rights abuses and whose family still holds considerable sway in Sri Lanka, promised to make a comeback at local council elections.
"There is no doubt at all that the people who stood by us since 2015 will continue to support us in the future as well," he said addressing his close associates.
"We will bring the forces opposed to the country down to their knees by organising the people."
His aides said he was returning a fleet of limousines he had used since his disputed appointment.
The country has been heading for a government shutdown as parliament failed to approve spending for 2019, and credit rating agencies downgraded its debt amid fears of a sovereign default.
There were doubts about the country's ability to repay $1.5 billion (Dh5.5 million) due to bond holders by January 10 without a legally constituted administration.
Some factions within Sri Lanka's parliament have pushed for Sirisena to be investigated for orchestrating an alleged coup.