An Islamist militia was driven out of the city of Benghazi early today in a surge of anger 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, which some US and Libya officials blame for the attack on the US consulate in Benghazi last week in which the US ambassador and three other Americans were killed, said it had evacuated its bases "to preserve security in the city".

In a dramatic sign of Libya's fragility, after sweeping through Ansar's bases 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.

The invasion of Ansar al-Sharia's compounds, which met little resistance, appeared to be part of a sweep of militia bases by police, troops and activists following a large demonstration against militia units in Benghazi yesterday.

Demonstrators pulled down militia flags and set a vehicle on fire inside what was once the base of Gaddafi's security forces.

Hundreds of men waving swords and even a meat cleaver chanted "Libya, Libya", "No more al-Qa'ida!" 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.

Libya's government had promised Washington it would find the perpetrators of what appeared to be a well planned attack on the US consulate, which coincided with protests against an anti-Islam video and the anniversary of the 9/11 attacks.

The attack and the outrage directed at the United States over the video across the Muslim world raised questions about President Barack Obama's handling of the so-called Arab Spring.

Although Ansar al-Sharia denies any role in the consulate attack, the latest events in the cradle of Libya's revolution appeared at least in part to vindicate Obama's 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."

Libyan political scientist Ahmad al-Atrash told Reuters:

"People in Benghazi and all over Libya want to get these militias under control ... The overwhelming feeling is against any element that keeps the situation unstable."

The second half of last night's protest proved his point.

Continuing to chant anti-Ansar slogans, the crowd, swelling into the thousands, moved on to attack a separate compound where the powerful pro-government Rafallah al-Sahati militia, safeguarding a big weapons store, opened fire on the assailants.

As looters later tried to leave the scene, vigilantes wielding clubs and machetes tried to prevent them driving off with heavy weapons.

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.

Police found six more dead bodies near the compound this 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."

Nasser Abdelhaaq, a Rafallah al-Sahati commander, said the brigade had returned to their compound this morning.

He suggested that the crowd had been deliberately manipulated to turn on Rafallah al-Sahati, an officially approved militia that also has Islamist leanings.

"Twenty-five per cent of those who came were there as saboteurs," he said. "Some of them, we know who they are, they were working with Gaddafi's security brigades."

While the compound was being looted, the government texted Benghazi mobile phones asking citizens to go home and "not allow saboteurs to destroy your noble and successful demonstration".

Libya's new rulers know that, while militias pose the biggest threat to their authority, the state's weak security forces rely on former rebel units, armed with heavy weapons, that fought in the uprising.

Like the rest of Libya, Benghazi is still prowled by dozens of armed groups operating openly, usually with the official permission of a government that is powerless to stop them.

Ansar al-Sharia's overt Benghazi presence was never huge. But it was one of the few groups operating openly without official licence.

Its leaders proclaim democratic government to be incompatible with Islam, and the presence on the streets of pickup trucks bearing their Kalashnikov logo was an affront to the government's authority.

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.

The attack on the US consulate seems to have provided a strong impetus for local authorities to rally support behind the weak national government.

Thousands of Libyans marched in yesterday's "Rescue Benghazi Day" in support of democracy and against Islamist militias.

"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."

US Ambassador Christopher Stevens was well liked, and many Libyans condemned the attack on the consulate despite being angered by the anti-Islamic US-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."