An Islamist militia was driven out of the city of Benghazi early on Saturday in a surge of protest against the armed groups that control large parts of Libya more than a year after the overthrow of Muammar Gaddafi.
A spokesman for Ansar al-Sharia said the group had evacuated its bases in Benghazi "to preserve security in the city".
In a dramatic sign of Libya's fragility, after sweeping through the base the crowd went on to attack a pro-government militia, believing them to be Islamists, triggering an armed response in which at least 11 people were killed and more than 60 wounded.
Ansar al-Sharia has been linked to the attack on the U.S. consulate in Benghazi last week in which the U.S. ambassador to Libya and three other Americans died, although the group denied involvement.
The invasion of its compound, which met little resistance, appeared to be part of a coordinated sweep of militia bases by police, government troops and activists following a mass public demonstration against militia units in Benghazi on Friday.
Demonstrators pulled down militia flags and set a vehicle on fire inside what was once the base of Gaddafi's security forces who tried to put down the first protests that sparked last year's uprising.
Hundreds of men waving swords and even a meat cleaver chanting "Libya, Libya", "No more al Qaeda!" and "The blood we shed for freedom shall not go in vain!"
"After what happened at the American consulate, the people of Benghazi had enough of the extremists," demonstrator Hassan Ahmed said. "They did not give allegiance to the army. So the people broke in and they fled.
"This place is like the Bastille. This is where Gaddafi controlled Libya from, and then Ansar al-Sharia took it over. This is a turning point for the people of Benghazi."
Adusalam al-Tarhouni, a government worker who arrived with the first wave of protesters, said several pickup trucks with Ansar fighters had initially confronted the protesters and opened fire. Two protesters were shot in the leg, he said.
"After that they got into their trucks and drove away," he said. Protesters had freed four prisoners found inside, he said.
Libya's government had promised Washington it would find the perpetrators of what appeared to be a well planned attack on the U.S. consulate, which coincided with protests against an anti-Islam video and the anniversary of the 9/11 attacks.
The consulate attack and the outrage directed at the United States over the video across the Muslim world have raised questions about President Barack Obama's handling of the so-called Arab Spring.
The latest events in the cradle of Libya's revolution appeared at least in part to vindicate his faith in Libya's nascent democracy.
"The killing of the ambassador, and a preceding set of serious security incidents, are a wake-up call to the new government to actually start to improve security," said Oliver Miles, former British ambassador to Libya.
"And now they've got backing from the street in Benghazi to do just that."
But the lack of central control remains a recipe for chaos.
Continuing to chant anti-Ansar slogans, the crowd, swelling into the thousands, moved on to try to storm a separate compound where the powerful pro-government Rafallah al-Sahati militia was entrusted with guarding a big weapons store, and opened fire on the assailants.
Looters carried weapons out of the compound as men chanted: "Say to Ansar al-Sharia: 'Benghazi will be your inferno!'"
Officials at three hospitals told a Reuters correspondent they had a total of five dead and more than 60 wounded from the night's violence.
A trail of blood near two abandoned cars led police to six more dead bodies near the Rafallah al-Sahati compound on Saturday morning, police officer Ahmed Ali Agouri said.
"We came as peaceful protesters. When we got there they started shooting at us," student Sanad al-Barani said. "Five people were wounded beside me. They used 14.5 mm machineguns."
The withdrawal of Ansar al-Sharia across Benghazi and the huge outpouring of public support for the government suggests an extraordinary transformation in a country where the authorities had seemed largely powerless to curb the influence of militia groups armed with heavy weapons.
Nevertheless, Ansar al-Sharia and other Islamist militias have bases elsewhere in eastern Libya, notably around the coastal city of Derna, known across the region as a major recruitment centre for fighters who joined the wars in Iraq, Afghanistan and Syria.
Thousands of Libyans had marched in Benghazi on Friday in support of democracy and against the Islamist militias that Washington blames for the assault on its consulate. Hundreds of Ansar al-Sharia supporters held their own protest.
Friday's "Rescue Benghazi Day" demonstration called for the government to disband armed groups that have refused to give up their weapons since the NATO-backed revolution last year.
"It's obvious that this protest is against the militias. All of them should join the army or security forces as individuals, not as groups," student Ahmed Sanallah said. "Without that there will be no prosperity and no success for the new Libya."
Although the main demands of the marchers did not mention the attack on the U.S. consulate, it seems to have provided a strong impetus for the authorities to rally support behind the weak national government.
One banner at the Ansar al-Sharia demonstration read: "Day to rescue Benghazi or day to rescue America?"
U.S. Ambassador Christopher Stevens was well liked, and many Libyans condemned the attack on the consulate despite being angered by the anti-Islamic U.S.-made film that triggered it.
Some protesters' placards read: "We demand justice for Stevens" and "Libya lost a friend." Others had mixed views.
"I am out today to defend Benghazi. Killing the ambassador is a completely separate thing," said 26-year-old Amjad Mohammed Hassan, a network engineer. "I don't give a damn about the killing of the ambassador because the Americans offended the Prophet. I am just here for Benghazi."
Benghazi, 1,000 km (600 miles) from Tripoli across largely empty desert, is controlled by various armed groups, including some comprised of Islamists who openly proclaim their hostility to democratic government and the West.