TRIPOLI (Reuters) - Hundreds of migrants have been relocated from government-run detention centers in Libya after being trapped by clashes between rival groups, U.N. and aid sources said on Thursday.

Slideshow ( 6 images )

The migrants were abandoned when their guards fled from the fighting, which killed almost 30 people. It pitted rival groups vying for power and state funds, a recurring theme in Libya since the chaotic overthrow of Muammar Gaddafi in a 2011 NATO-backed uprising.

The migrants were taken to a “safer place” from two centers run by the U.N.-backed government in the Ain Zara area in southeastern Tripoli, aid workers said.

The U.N refugee agency UNHCR said in a statement it facilitated the transport “in coordination with other agencies and the Department for Combatting Illegal Migration (DCIM)”.

The migrants, mainly Eritreans, Ethiopians and Somalis, were taken to a separate detention center away from the fighting. A few were still waiting to evacuate from Ain Zara, an official at another international organization said.

Libya is the main departure point in North Africa for migrants crossing the Mediterranean Sea to Europe, mainly from other parts of Africa.

The number of crossings has fallen off sharply since Italy provided the coastguard with more boats and brokered deals with local groups in a smuggler hub last year.

A total of 27 people have been killed and 91 wounded, mostly civilians, since the outbreak of the fighting, the health ministry said in a statement on Thursday.

Tripoli-based Prime Minister Fayez al-Serraj said there was still fighting in southern Tripoli where residents have been trapped inside their homes.

Tripoli is formally controlled by the internationally recognized Government of National Accord, but armed groups working with it act with autonomy. Eastern Libya is controlled by a rival administration.