You are not permitted to download, save or email this image. Visit image gallery to purchase the image.
At least five people were wounded and a stray bullet entered a hospital in the heart of the city, where residents rushed to arm themselves, saying calls to police had gone unheeded. After more than 12 hours, the army moved in.
The violence underscored the challenge faced by Libya's first freely-elected government, approved just last week, to rein in militias which gained power during the conflict that ended Gaddafi's 42-year rule a year ago and hold together a country riven with clan, regional and sectarian divisions.
By early afternoon, a building belonging to the Supreme Security Committee (SSC), a body set up last year to try to regulate armed groups, was in flames and being looted by members of a rival militia faction, witnesses said. A sports shop that helps fund one of the militia groups was also looted.
The fight erupted just after midnight after a dispute over the detention of a member of one of the armed groups, residents in the southern district of Sidi Khalifa said.
Both militias are affiliated to the SSC, an umbrella group for various armed groups that refused to join the official police or army, saying they were still run by Gaddafi loyalists.
Civilians blocked the street where the fighting raged to prevent cars entering the battleground where the sound of gunfire rang out. Many civilians went home to get their own arms.
"We called the police early in the morning to help us stop the shooting, but no one came," resident Khaled Mohamed told Reuters.
A stray bullet caused panic at the nearby Tripoli Central Hospital, sending doctors and nurses running for cover. Dr. Khaled Ben Nour said five casualties had been brought in.
"We have real patients with real needs. These rogue militias need to leave us in peace so we can do our jobs," Ben Nour said.
Some fighters said the clash was over the detention of the militia member while others said the SSC headquarters - a former intelligence building - had been occupied by a militia called Support Unit No. 8 led by Mohamed al-Warfali.
A group of rival militias - also belonging to the SCC - fired at the building from a former post office.
"Mohamed al-Warfali and his lawless group of men have occupied the SSC building and refuse to come out," said m ilitia member Mohamed al-Himrazy who accused Warfali's group of breaking SSC rules.
Others said that a militia group came to free a member detained by Warfali in the SSC building.
The clash highlighted the dangerously chaotic nature of the semi-official militias in Libya which hold a great deal of power and have loyalties sometimes at odds with the government's.
At nightfall, as the shooting was dying down, around 20 army pick up trucks mounted with anti-aircraft surrounded the SSC building and army officials went in, retrieving papers and binders of documents.
The SSC, run and paid for by the Interior Ministry, is much better armed that the official police. The ministry has repeatedly promised to disarm the militias but has yet to do so, to the disgust of many Libyans.
"The government needs to find a solution for this security mess," said resident Khaled Ahmed.
"It's been two years since the revolution and there is still no security. They either need to find a solution or we take to the streets again."
Apart from draining public finances, SSC members have been accused of kidnappings and intimidation across the country. For their part, the armed revolutionaries who fought the war feel unrepresented by the elected civilian members.
About 2 km (1.2 miles) from the gun battle, members of the General National Congress debated whether the new government should be sworn in on Thursday and whether they should move to a different city because of recurrent attacks on their building.
On Wednesday, security forces fended off protesters outside the congress building as a cabinet line-up led by Prime Minister Ali Zeidan was finally approved following Libya's first free elections in July. Protesters said some of the nominees had past links with the Gaddafi regime.
On Sunday, violence also erupted outside the capital.
A car bomb exploded in front of a police station in Benghazi, injuring three police officers in the latest in a series of attacks on security officials in Libya's second largest city, where the U.S. ambassador was killed in September.
The front of the central Hadayeq police station was charred and blackened with smoke. The entrance to the station was destroyed, with glass strewn on the street and firefighters putting out a fire engulfing a police car.
A Reuters photographer saw three policemen receive first aid for minor injuries in front of the station. Officials at the scene said there were no deaths.