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

The withdrawal is a triumph for the residents of Tripoli, who on Nov. 15 held a mass protest against the militias, which have fueled lawlessness across Libya since the fall of longtime autocrat Moammar Gaddafi in 2011.

The heavily armed groups, some of them led by Islamist 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 Tripoli. They kept their weapons, mostly assault rifles, antiaircraft guns mounted on pickup trucks and rocket-propelled grenades. The militias that left come from four cities, including Zintan, to the south of Tripoli, and Misurata to the west.

Government moves to rein in militias, however, risk an explosive backlash leading to outright battles between the rival militias. Because many of them are rooted in specific cities and act as arms of political groups, any violence could pit city against city in the already fragmented nation.

Libya’s military swept into Tripoli on Monday to drive out the militiamen. The troops were met with a warm welcome from Libyans angry about the numerous armed groups running rampant in the country.

The sweep was the most assertive yet by the military, but the government’s armed forces and police remain weak and rely on allied militias for firepower.

Prime Minister Ali Zeidan urged Libyans on Thursday to respect the army and police and heed their orders. He said he intends to submit draft legislation to parliament that criminalizes the illegal possession of arms — targeting unruly militias.

— Associated Press