Libya's supreme court Monday ruled as unconstitutional the election of premier Ahmed Miitig in a chaotic parliamentary session, ending a month-long political crisis that saw two rival cabinets jostling for power.
Miitig said he would respect the ruling, hailing the decision as a "boost for the conservation of the rule of law" in Libya.
The standoff started when parliament in early May voted Miitig as new premier to replace Abdullah al-Thani, who resigned after an attack on his family.
Thani however refused to recognise the parliamentary vote, which came days after gunmen stormed the building to interrupt an earlier ballot.
Several liberal lawmakers accused Islamist blocs within the interim parliament 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 had garnered only 113 votes.
Thani insisted e 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 rival premiers 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 (the interim parliament) as unconstitutional," a judge at the court said after a short hearing, without elaborating.
Miitig, 42, an independent backed by the Islamists, had been due to lead the country for a short interim period until June 25, when the country is due to hold an election to replace the 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 that 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 press conference shortly after the court ruling.
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 political standoff between the rival cabinets amid rising unrest across the country allowed rogue general Khalifa Haftar to press an offensive against Islamists in the restive eastern city of Benghazi.
Haftar launched the attack, dubbed "Operation Dignity", last month with troops from his so-called National Army. He has rallied support among the public and members of the security forces have joined his forces.
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. No group has claimed the attack, but they have been blamed on radical Islamist militias based in the city.
Benghazi was relatively calm over 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 autonomy for eastern Libya, told AFP he was satisfied with the Supreme Court judgement.
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 that ousted dictator Moamer Kadhafi in 2011.
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 Kadhafi's ouster.
Its legitimacy was challenged after the GNC prolonged its mandate, due to expire last February, until December 2014.