Militias from a string of Libyan cities have left the capital, Tripoli, nearly a week after more than 40 people protesting their presence in the city were killed by militiamen.

Thursday's withdrawal is a triumph for the residents of Tripoli, who on Nov. 15 held a mass protest against the militias, which have fueled lawlessness nationwide since the 2011 fall of longtime leader Moammar Gadhafi.

The heavily armed groups, some of them led by Islamic extremists, have defied control by the weak central government, carving out fiefdoms and acting as a law unto themselves.

Witnesses said the militiamen gave their bases to army troops in handover ceremonies before they headed out of the city with their weapons, mostly assault rifles, anti-aircraft guns mounted on pickup trucks and rocket-propelled grenades.