The fighting fuelled growing fears that nobody is in control of thousands of swaggering armed men who are still based in Tripoli and that the country's interim government will struggle to impose law and order.
Two people died from bullet wounds and at least seven fighters were injured during a battle that started when militia from the town of Zintan were stopped by guards from the Tripoli Brigade from entering the city's Central Hospital to kill a patient.
The hospital front door and entrance hall were afterwards left pocked with bullets, doctors and patients had to flee the building and two elderly patients died of heart attacks during the shooting, which lasted from about 1am until dawn. Heavy machine guns and anti-aircraft guns were used by both factions, supposed allies who in reality nurse a dangerous rivalry.
The shoot-out started when a group of gunmen arrived at the hospital in search of a man they had shot earlier in the night. Witnesses said the gunmen were drunk, and had come to finish the man off after learning that he had survived and been taken for medical treatment.
Doctors asked them to leave, at which point one of them pulled out a pistol and began shooting.
"He was overpowered, but then hundreds of Zintan men arrived outside the hospital with heavy weapons and shooting started," said Mohamad Hamza, a Tripoli Brigade fighter in charge of security. "We had to call for backup, and our boys came from all over Tripoli.
"We couldn't believe that they were shooting at us. I had to say to them, you are shooting at a hospital, not at Muammar Gaddafi's 32nd Brigade. Eventually, after several hours, a Shaikh came from the mosque and persuaded them to stop and they handed over three of them who started it to Tripoli's military council."
He said one Zintan fighter and a passer-by were killed in shooting, and seven Tripoli Brigade men were injured, two seriously. He said he believed Zintan injured were taken to other hospitals.
The incident will raise pressure on the fragile National Transitional Council to disarm the former rebel fighters who are still at large in Libya's capital, even though they were asked to leave weeks ago and have been ordered to give up their heavy weapons.
The Zintan brigades were some of the most ferocious fighters against Gaddafi's forces and helped lead the attack on Tripoli, but have outstayed their welcome, earning a reputation for mayhem and looting.
Thousands of them have ignored pleas to go, staying put instead of returning to their town in the mountains three hours drive to the south.
The battle came on the day Human Rights Watch warned in a report that the entire population of 30,000 people from the town of Tawargha, near Misurata, has been driven out by former rebels for siding with Gaddafi. There have been reports that some of its men, who are predominantly black-skinned, may have been shot or beaten.
Tripoli's residents fear that there will be more clashes in their city, which is desperate to get back to normality. Mr Hamza, in charge of security at the hospital, said he expected more trouble. "I think it will happen again," he said. "They will be back for revenge."
Peter Cole, Libya analyst with the International Crisis Group, said: “Rivalry between brigades from different cities has not been resolved and it does now pose a threat to Libya’s security.
“This suggests that the National Transitional Council needs to work harder with the militia groups to bring unity among them.”
The fighting came on the day that Nato formally ended operations in Libya.
The military action, unprecedented in setting out from the beginning to win a war while guaranteeing not to use troops to do so, was declared a success by Nato’s chief. “At midnight tonight, a successful chapter in Nato’s history is coming to an end,” Anders Fogh Rasmussen, the Nato secretary-general and a former Danish prime minister, said at a press conference with the interim Libyan president, Mustafa Abdul Jalil.
Libya’s interim leadership meanwhile elected an academic from Tripoli as the country’s new interim prime minister.
Abdel-Rahim al-Keeb was chosen by the National Transitional Council and will appoint a new Cabinet in coming days. The new government is to run Libya in the coming months and to pave the way for general elections.