Clashes continued for a second day among heavily-armed militias in the Libyan capital Tripoli, as they fought for control over the city in what appears to be the worst outbreak of violence the city has seen in two years.

Competing militias have chopped Tripoli up into fiefdoms and power centres after Muammar Gaddafi, Libya's ruler for 42 years, was ousted and killed in an uprising in 2011.

They maintain what observers describe as a "balance of terror".

Witnesses in Tripoli said on Friday that gun battles rocked the south-eastern Nasr Forest district and adjacent neighbourhoods as residents were advised by a local emergency body affiliated with the Interior Ministry to remain at home and away from windows.

The clashes started Thursday and have reportedly killed at least eight, according to state news agency LANA. During a lull in the violence late Thursday night, panicked residents could be seen lining up outside petrol stations to stock up on fuel.

In a statement Friday, UN envoy to Libya Martin Kobler called for a halt to the fighting, saying he is "extremely alarmed".

"It's completely unacceptable for armed groups to fight to assert their interest and control, particularly in residential areas, terrorising the population," he said.

Reached by phone, a female resident in Tripoli told The Associated Press that families had locked themselves in their homes.

"We haven't slept all night and we haven't left the house since yesterday. All we are hearing is screaming, bombings and gunfire," she said. "The security situation is going from bad to worse."

"We just want the militias to leave," she pleaded.

The UN-brokered unity government's spokesman Ashraf al-Thilthi said a ceasefire agreement has been reached among warring militias but gave no further details.

Given the fluidity of the situation, it remains to be seen whether the victors of the clashes will back the internationally-recognised body.

"This is a struggle over power. Each of the warring parties has its political and ideological agendas," said Sami al-Atrash, a Tripoli resident and a legal expert. "The clashes are belated. They were expected at any moment and finally happened," he said.

In March this year the prime minister of the unity government, Fayez Sarraj, arrived quietly by boat in Tripoli from Tunisia. Militias in control of the capital showed no resistance.

One of Tripoli's warring sides is supportive of Sarraj and is made up of mainly local militias, such as the ultraconservative Salafist forces led Abdel-Raouf Kara, known as the Special Deterrent Force, or "Rada" in Arabic.

One of Tripoli's largest and most powerful militias, Tripoli Revolutionaries, led by Haytham al-Tajouri, had previously been ambivalent about the unity government, but it has now joined forces with the Rada militia.

The other warring group is more sceptical of the unity government. Some hail from the western city of Misrata, while others are linked to the former leader of the one-time Jihadist but now defunct Libya Islamic Fighting Group, by Abdel-Hakim Belhaj.

They are backed by the hard-line Grand Mufti Sadek al-Ghariani. Both Belhaj and the Grand Mufti are believed to be receiving financing from Qatar.

The two sides have been exchanging accusations of orchestrating killings and kidnappings. The latest dispute involved an aide to al-Ghariani, Nader al-Omrani, whose body was found weeks after he was abducted, and the Rada militia was blamed for his killing.

On Thursday night, the Rada and Tripoli Revolutionaries militias took control of five-star hotel Al Mahary Radisson Blu Hotel in central Tripoli, where Belhaj had turned some of its rooms into offices. The same alliance also took over the 6th Division barracks once occupied by rival militias.

Fighting also broke out in the vicinity of the Rixos hotel, in the capital's south east - its convention centre was once the meeting venue for the outgoing parliament but has since changed hands.

Pictures posted by activists on social media showed militiamen firing RPGs from rooftops, and armoured vehicles and pickup trucks mounted with anti-aircraft weaponry occupying main streets.

The fighting is believed to be the worst in two years, following Libya's last parliamentary elections when Islamists were dealt an unexpected blow, and the anti-Islamist head of the armed forces general Khalifa Hifter launched an offensive against Islamic militants in the eastern city of Benghazi.

Friday's fighting came amid unconfirmed reports that the UN would make substantial amendments to the current political agreement to give Hifter a share of power, which could anger his rivals in Tripoli and Misrata.

Sign up to our free newsletters

Get the best updates straight to your inbox:
Please select at least one mailing list.

You can unsubscribe at any time by clicking the link in the footer of our emails. We use Mailchimp as our marketing platform. By subscribing, you acknowledge that your information will be transferred to Mailchimp for processing.