An urgent evacuation plan has been devised for migrants facing abuse in Libyan detention camps.
It was drawn up at an African Union-European Union summit in Ivory Coast.
Libya’s UN-backed administration joined the agreement, but has only limited control over the territory, raising questions about how it will work in practice.
The migrants will be sent mainly back to their home countries.
The move follows the publication of video footage that appeared to show migrants from sub-Saharan Africa being sold in Libya as slaves.
French President Emmanuel Macron called the slave auctions a “crime against humanity”.
Hundreds of thousands of migrants have crossed the Sahara and the Mediterranean trying to reach Europe.
Thousands die on the journey and those who make it arrive in Libya virtually penniless, making them vulnerable to modern-day slave traders.
Libya launched a formal investigation into migrants being sold off as farm labourers for as little as $400 (£300) after CNN broadcast footage of the slave auctions in mid-November.
Nigeria had already made a unilateral move to repatriate migrants, with 240 voluntarily flown home on Tuesday night.
Ghana also repatriated more than 100 of its citizens detained in Libya.
Speaking in Abidjan, Mr Macron said the “extreme emergency operation” had been agreed by nine countries, including Libya, France, Germany, Chad and Niger.
Libya had reiterated its agreement “to identify the camps where barbaric scenes have been identified”, he said according to AFP news agency.
Libya’s UN-backed Prime Minister Fayez Sarraj had agreed to give the International Organization for Migration (IOM) access to the camps, where it would offer migrants the chance to be evacuated in the coming days and weeks.
“This work will be carried out in the next few days” in co-ordination with the countries of origin, Mr Macron said – adding that particularly at-risk migrants could be given asylum in Europe.
EU sources told AFP that humanitarian organisations, including the IOM, had already repatriated some 13,000 migrants in the last year, mainly to sub-Saharan Africa.
Analysis: Ambitious plan
By Rana Jawad, BBC North Africa correspondent, Tunis
The summit’s focus was supposed to be on African youth, but it turned out to be a conference about migration. The aim is to get more migrants from Libya repatriated to their home countries, as quickly as possible. The evacuations will be carried out by the IOM and funded by the EU, a practice that has been in place for years.
A European MP who was at the summit told me the IOM suggested that it could repatriate some 15,000 people before Christmas – it’s ambitious, but unrealistic, considering the numbers flown out over the past year.
I am also told that they will have to pay for exit visas. The idea is to get people to return voluntarily, and African embassies in Tripoli and Tunis will need to help coordinate this.
At its core, this plan appears to be part of the EU’s wider strategy to reduce Mediterranean crossings, but it now comes against the backdrop of global outrage against a slave trade in Libya that has been known to member-states for more than a year and which the IOM highlighted in a report in March this year.
The logistical obstacles to this latest plan have not changed – the Libyan government is still largely incapable of controlling any migrant detention centres or camps overseen by militias.
The AU-EU meeting also agreed other initiatives to target traffickers, including a task force to dismantle trafficking networks and freeze assets.
But bodies including the AU and UN have accused the European Union of helping to create the conditions for migrant abuse in Libya with their policy of intercepting Europe-bound migrants and returning them to Libya.
And the IOM’s chief of mission in Libya, Othman Belbeisi, has warned smuggling networks in Libya are “getting stronger”.