Vehicles of forces loyal to Libya s Tripoli-based Prime Minister Abdulhamid Dbeibah are parked along the waterfront in the capital Tripoli on May 17, 2022. AFP
The side backed by an eastern-based military strongman then pulled out of the western city of Tripoli, citing the "security and safety of citizens", as the United Nations and European Union launched urgent appeals for calm.
There were no immediate reports of casualties from the fighting that erupted before dawn, according to AFP correspondents.
Libya has been ruled by Prime Minister Abdulhamid Dbeibah, but he has been challenged by Fathi Bashagha, who was named as rival premier in February by the parliament in the country's eastern city of Tobruk and is backed by strongman Khalifa Haftar.
Violence flared before dawn after Bashagha's press service announced "the arrival of the prime minister of the Libyan government, Mr Fathi Bashagha, accompanied by several ministers, in the capital Tripoli to begin his work there".
The fighting raised fears of a return to the chaos that reigned after a NATO-backed popular revolt in 2011 toppled dictator Moamer Kadhafi, and to the all-out conflict that gripped the capital in 2019-20.
After several hours early Tuesday, Bashagha's camp announced that he and his ministers "have left Tripoli to preserve the security and safety of citizens".
There was no immediate reaction from the unity administration led by Dbeibah, which was installed in 2020 as part of a troubled UN-backed peace process and which has since refused to step down.
- 'Dialogue and mediation' -
The UN's top in-country official, Stephanie Williams, in a tweet called for calm and stressed the necessity of "refraining from provocative actions including inflammatory rhetoric, participation in clashes and the mobilisation of forces.
"Conflict cannot be solved with violence but with dialogue and mediation."
Dbeibah's government was tasked with leading Libya to elections scheduled for last December, but these were indefinitely postponed and his political opponents argue that his mandate has now finished.
In video footage broadcast by Libyan television stations, Bashagha meanwhile said he had "received a very warm welcome" in Tripoli.
He announced that he would hold a news conference on Tuesday evening during which he would deliver a a "unity speech to the Libyan people".
Bashagha's interior minister Issam Abu Zariba pledged Tuesday that their administration would assume its duties "peacefully" and "in compliance with law".
He called on "all security forces and concerned parties to cooperate" in ensuring a smooth transition.
Pro-Bashagha armed groups had already deployed in March on the edges of the capital, raising fears of a confrontation that would end a fragile ceasefire in place since October 2020.
- 'Very worrying' -
Libya, a vast country of seven million, was plunged into violent lawlessness in 2011 with the NATO-backed revolt that toppled Kadhafi.
Armed groups have vied for control of territory as a string of interim governments have come and gone.
Many such groups have been integrated into the state, partly in order to access a share of the country's vast oil wealth, and human rights organisations have often accused them of abuses.
The creation of two governments echoes Libya's troubled period of rival administrations between 2014 and 2021, when the nation was ripped apart by civil war.
Oil production, the country's main source of income, has again been hit by political rifts with a wave of forced closures of oil terminals by groups aligned with the eastern camp, who want power transferred to Bashagha.
Bashagha is backed by Haftar, who maintains control of several key oil installations.
EU foreign affairs chief Josep Borrell voiced alarm Tuesday, saying that "the situation in Libya has become very grave in the last hours.
"We were expecting something like this to happen because in Libya we had not elections but we have two governments," he added.
"And sooner or later, when there are two governments, they clash ... It is very worrying what's happening."