Libya's supreme court Monday ruled as unconstitutional the interim parliament's election of premier Ahmed Miitig, seemingly ending a month-long crisis that saw two rival cabinets jostling for power.
Miitig said he would respect the ruling, hailing it as a "boost for the conservation of the rule of law", while a UN envoy welcomed the decision but warned Libya's deeper crisis was not over.
The standoff started when the General National Congress, or interim parliament, elected Miitig premier in early May to replace Abdullah al-Thani, who had resigned after an attack on his family.
But Thani refused to recognise the vote, which came days after gunmen stormed the GNC building to interrupt an earlier ballot.
Liberal lawmakers accused Islamist blocs of allowing late arrivals at the session to cast their votes after the initial result was announced to make up the 121 votes needed, after Miitig garnered only 113 votes.
Thani insisted he would await a decision by the judiciary before handing over power.
But Miitig convened his first cabinet meeting last week despite Thani's objections, and the two disputed power in Tripoli, laying claim to the largely lawless North African nation's huge reserves of oil and gas.
On Monday, the Supreme Court issued its ruling.
"The court has judged the election of Miitig at the General National Congress as unconstitutional," a judge said, without elaborating.
Miitig, 42, an independent backed by the Islamists, had been due to lead Libya for a short interim period until June 25, when it is to elect a new Congress.
Constitutional law expert Abdelgader Gdoura told AFP the "Supreme Court's decision is final... Miitig's government is finished".
The GNC had also said it would comply with the decision, and confirmed Thani would head the interim government.
"The congress complied with the judiciary's decision," Salah al-Makhzum, a vice president of the GNC, told a news conference shortly after the court ruling.
- Simmering standoff -
The head of the UN mission to Libya, Tarek Mitri, welcomed the ruling as "important" but warned security had deteriorated, particularly in the east.
Signup to our newsletter and follow us on Facebook and Twitter!
"This would not solve the political crisis but at least it would open the way forward towards the resumption of a normal political process," Mitri told reporters in New York.
Asked how security in Libya compared to six months or a year ago, he said: "The situation has deteriorated, especially in the east. There is no doubt about that."
Near daily attacks in the eastern city of Benghazi, cradle of the 2011 revolt against dictator Moamer Kadhafi, have killed dozens of members of the security forces.
Thani announced his resignation earlier this year after an armed attack on his family, but insisted his successor be chosen by a new parliament rather than its contested predecessor.
After refusing to hand over power, Thani convened his cabinet last week even as Miitig's government held its first session, reportedly in a luxury hotel since his predecessor was at the time occupying the seat of government.
The standoff amid rising unrest across Libya allowed rogue general Khalifa Haftar to press an offensive launched last month against Islamists in Benghazi.
Benghazi was relatively calm at the weekend, but an exchange of rocket fire between Haftar's forces and the Islamists killed two civilians in the suburbs.
Some politicians and armed groups in the country had warned they would not endorse Miitig's government, including autonomist rebels who have been blockading eastern oil terminals.
Ibrahim Jodhran, self-declared head of the Cyrenaica Political Bureau, a group demanding greater eastern autonomy, told AFP he was satisfied with the Supreme Court judgment.
- Judiciary hailed -
He hailed the move as proof of the "integrity and independence of the Libyan judiciary".
Miitig would have been Libya's fifth premier since the revolution.
Successive governments in Tripoli have failed to stamp their authority on militias that fought Kadhafi and have refused to surrender their arms or join the regular army.
The GNC was elected in July 2012, in Libya's first ever free polls, almost one year after Kadhafi's ouster.
Its legitimacy was challenged after the GNC prolonged its mandate, due to expire last February, until December 2014.