* Headquarters of Islamist Ansar al-Sharia group targeted
* Group was linked to last week's attack on U.S. consulate
* Libyans march in Benghazi to support democracy
* Four killed, 34 wounded
By Peter Graff and Suleiman Al-Khalidi
BENGHAZI, Libya, Sept 22 (Reuters) - The powerful Islamist militia Washington blames for an attack on its Benghazi consulate was swept from its heavily fortified bases in Libya's second city in a mass popular uprising in support of the government early on Saturday.
The action against Ansar al-Sharia appeared to be part of a coordinated sweep of militia headquarters buildings by police, government troops and activists following a mass public demonstration against militia units on Friday.
At least four people were killed and 34 wounded, hospital sources said, as militants fought demonstrators. Gunfire could be heard in the area before the fighters were forced out.
Looters carried weapons out of the vacated Ansar al-Sharia military base compound as men clapped and chanted: "Say to Ansar al-Sharia, Benghazi will be your inferno."
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. It denies involvement.
Chanting "Libya, Libya", "No more al Qaeda!" and "The blood we shed for freedom shall not go in vain!" hundreds of men waving swords and even a meat cleaver, stormed Ansar al-Sharia's headquarters in Benghazi.
"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."
Demonstrators pulled down militia flags and set a vehicle on fire inside what was once the base of former leader Muammar Gaddafi's security forces who tried to put down the first protests that sparked last year's uprising.
"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," said Ahmed.
Adusalam al-Tarhouni, a government worker, who arrived with the first wave of protesters, said several pickup trucks with the group's 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.
As protesters left Ansar al-Sharia's headquarters, the crowd swelled, reaching thousands as it headed toward the Islamists' military base, which was shared with another militia group.
Protesters said the militiamen opened fire as they arrived and several people were wounded.
"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."
After the crowd entered that compound, Libyan army trucks sped away from the base carrying government troops cheering in victory and crying out, "God is greatest."
Vigilantes armed with machetes and clubs blocked the highway leading away from the compound, stopping cars to prevent looters from driving off with heavy weapons.
"We went into the camp and we didn't find anyone. We just took these Kalashnikovs," said one youth, holding rifles.
The demonstrators also took over a compound belonging to the Abu Slim brigade and another Ansar al-Sharia compound.
The apparent defeat of Ansar al-Sharia across Benghazi and the huge outpouring of public support for the government marks 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 militia 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 country's weak government.
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.
Some are identified by local people as being among those who were at the consulate protest last week. U.S. officials have described the violence as a "terrorist attack."
Abu Al-Qaa, a demonstrator at the Ansar al-Sharia demonstration, said Stevens had been "preparing for the entry of American troops into Libya".
"The will of the Prophet was to expel infidels from Muslim lands so that Muslims prevail. Terrorising your enemy is one of Islam's tenets." He said he had fought U.S. troops in Iraq where he was arrested, sent back to Libya and jailed for three years.
One banner at the Ansar al-Sharia demonstration read: "Day to rescue Benghazi or day to rescue America?"