By Ghaith Shennib and Patrick Markey
TRIPOLI/TUNIS (Reuters) - Militia fighters blamed for the worst unrest in Tripoli since the fall of Muammar Gaddafi pulled out of the capital on Monday with Libyan army units taking up positions around the city.
More than 40 people were killed in street battles between rival militias in Tripoli last week, highlighting Libya's struggle to curb fighters and hardline Islamists who refuse to disarm two years after ousting Gaddafi.
The latest bloodshed has increased popular anger against militias in Tripoli, where rival groups have often clashed violently over territory or in personal feuds.
The withdrawal of one powerful set of fighters, though, may leave Libya's fragile government to face more competition among the militia groups that remain in the city.
Western powers, worried about anarchy in a major oil producer and further insecurity in the region, are promising more expertise to build up Libya's army.
The country is being closely watched by its North Africa neighbors, worried about violence spilling across porous borders, especially with al Qaeda militants sheltering in southern deserts where Tripoli has less influence.
The pull-out of the gunmen, who are aligned to the coastal city of Misrata, a center of anti-Gaddafi resistance, will show how far Libya's fledgling armed forces are able to control other militias in the capital, and test Prime Minister Ali Zeidan's will to reassert control over his vast North African country.
Militias from Misrata, including parts of the so-called Libya Shield Force and the Gharghour Brigades, withdrew from Tripoli on Monday, said Saleh Jouda, a member of the national security committee in the country's assembly.
Tripoli was calm with many stores, schools and universities closed in support of a strike called by the city's leaders to demand the militia gunmen leave.
"They have to understand that we want an army, police and rule of law," said Hisham Alwendy, an activist. "All militias should leave the city, even those who are from Tripoli itself."
That may be a tough task. Tripoli, like other regions, remains a dangerous patchwork of rival militia territories controlled by Islamist, secular and tribal gunmen.
Even the official armed forces, and defense and interior ministries, rely on former fighters for security under a program to employ the militiamen.
The prime minister was himself briefly abducted last month from the a five-star Tripoli hotel by gunmen nominally on the government payroll.
Militia fighters employed to guard oil sites have defected and managed to disrupt Libya's oil exports in recent months, cutting off the government's main source of revenue.
Popular protests have helped dislodge Libyan militias before. A hardline Islamist group was driven from Benghazi last year by armed protests, and residents upset by growing violence confronted part of the Libya Shield Force militia this year.
But in Tripoli, where militias have been carving out territories and influence since the fall of Gaddafi, dislodging them may not be so easy.
"There are some power plays to come about who will replace the militias; clearly it is a fragile situation," one western diplomat said. "It is also clear that they cannot go on delaying a decision on how to deal with the militias."
Misrata's militias were among the first to move into Tripoli as the war drew to a close in 2011. Misrata men are in the Libyan Shield Force linked to the defense ministry.
Across the city are the powerful Zintanis, more secular-leaning and mostly drawn from the Bedouin tribes. Their al Kaka brigade is among the country's most powerful militia groups.
Local militias from Tripoli including the Tajoura brigades and the Islamist-leaning Supreme Security Council, which are aligned with the interior ministry, have sought to position themselves as a legitimate force.
Militia rivalries mirror divisions in Libya's government, where the secular National Forces Alliance is deadlocked with a wing of the Islamist Muslim Brotherhood over the country's direction after Gaddafi's fall.
Competition among armed groups with regional and tribal agendas is expected to continue as long as Libya's transition to democracy, its constitution and the distribution of its vast oil reserves remain unresolved.
"Violence and insecurity are likely to remain a key feature of politics and society for years to come, at least until the new constitution is approved and fresh elections," said Riccardo Fabiani at Eurasia Group.
Even as Libya's new military takes the streets, it is clear it needs more training. Western powers, including the United States and France, have reiterated their support.
The U.S. military is working on plans to train 5,000 to 7,000 Libyan security forces.
(Reporting by Ghaith Shennib; Writing by Patrick Markey; Editing by Peter Graff and Giles Elgood)