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.
Migrants trapped in Libyan detention centres have described them as “like hell”.
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.