Lesotho’s scandal-hit prime minister will stand down immediately following an agreement between the coalition government and political parties in the southern African mountain kingdom, a joint statement said on Monday.

Thomas Thabane, who has been accused of involvement in the murder of his ex-wife, will be allowed a “dignified retirement”, according to the deal negotiated by mediators from neighbouring South Africa.

The 80-year-old leader appeared in court in February, where his lawyer argued that his office granted him immunity from prosecution for allegedly murdering his estranged first wife shortly before he took power.

Lipolelo Thabane, 58, was shot several times at close range as she sat in a car near her home, two days before her husband’s inauguration in 2017. She had reportedly refused a divorce and won a court battle to retain her privileges as first lady until any formal separation.

Thabane remarried two months after her death. His current wife, Maesaiah, 42, has been charged with murder after briefly fleeing the country. Both deny any wrongdoing.

The case was brought back into the spotlight in January after a letter from Lesotho’s police chief emerged claiming communication records showed that someone at the murder scene had called Thabane’s mobile phone on the day of the crime.

The killing was initially blamed on unknown criminals; Lesotho has one of the highest murder rates in the world. Investigators have since said they believe professional hitmen, possibly hired in South Africa, carried out the attack.

Thabane had already pledged to step down at the end of July, but the South African mediator Jeff Radebe told journalists in Lesotho’s capital, Maseru, that “the timeline is immediate” for his leaving office

The veteran politician made a final attempt to hold on to power at the weekend, sending soldiers and armoured vehicles on to the streets of Maseru on Saturday to restore order against “rogue national elements”. The move prompted authorities in South Africa, which surrounds Lesotho, to send envoys to the poor mountain kingdom of 2 million people.

Thabane said he had deployed the army to deal with forces he said wanted to destabilise Lesotho.

The country has experienced several coups since gaining independence from Britain in 1966. In 1998 at least 58 people and eight South African soldiers died and parts of Maseru were damaged during a political standoff and subsequent fighting.

The latest upheaval follows Thabane’s decision last month to suspend parliament without consultation over the coronavirus pandemic. Lesotho has no confirmed cases of Covid-19, and the move was challenged in the constitutional court by coalition partners as well as some 20 rivals within his own party.

The court ruled against the suspension on Friday, calling it “irrational”, paving the way for a threatened vote of no confidence against Thabane once parliament reconvenes.

“He has nowhere to hide and has lost it. The electorate is against him, the judiciary and all other strategic institutions … meaning that the PM is finished,” Sofonea Shale, an independent political analyst, said on Sunday.