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.
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.
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”.