Cheering crowds swept through the Libyan city of Benghazi clearing Islamist militias from their bases after protests triggered by the killing of the American ambassador, Chris Stevens.

Up to four people were reported dead in clashes which broke out when the last and biggest militia was attacked in the early hours of Saturday morning. Earlier, members of Ansar al-Sharia, the militant group accused of responsibility for Mr Stevens' death, were forced out of their strongholds in the city.

The deaths and promises of retribution by militia leaders presage more trouble to come. But for the moment the headquarters of the freelance groups that have held sway in Benghazi since the revolution that toppled Col Muammar Gaddafi are claimed to be in the hands of police and army units loyal to the newly elected prime minister, Mustafa Abushagur.

The protests in Benghazi on Friday evening, estimated at 30,000-strong, featured pro-American slogans and banners, unusual for demonstrations in Arab countries. Though many protesters said they were attending "for Benghazi, not for America", some held up placards commemorating Mr Stevens, who lived in Benghazi last year while co-ordinating American support for the revolution.

"We demand justice for Stevens," said one, and "Libya lost a friend" another.

The march was given the title "Rally to Save Benghazi". Its aim and that of another, smaller demonstration in Tripoli was to demand the government, which Mr Abushagur is still forming, take control of security in the country and disband the militias or co-opt them into the army under a unified structure.

Initial fears of violence between marchers and a counter-demonstration by members of Ansar al-Sharia proved unfounded as the Islamists were heavily outnumbered. They have denied killing Mr Stevens, but said he was preparing the way for American intervention in Libya, and also referred to the anti-Islam film circulating the internet.

But after the rally ended, some of the protesters, with police and army apparently in support, marched on Ansar al-Sharia bases. They cleared them from in front of the Al-Jala Hospital, which they had been guarding, so that by late evening the anti-aircraft guns mounted on pick-up trucks that had been the basis of hospital security had disappeared.

They then stormed the main compounds of the group, setting on fire to cars inside. The group, whose name translates as "Supporters of Sharia", fired in the air but did not mount a major resistance.

"Libya, Libya," the protesters shouted, saying they wanted to put an end to al-Qaeda and demanding that militia members sign up to the army as individuals, not allow their leaders to maintain their separate identity.