Six EU countries have agreed to take in 356 African migrants aboard the rescue ship Ocean Viking, which has not been allowed to dock in Italy or Malta.
The migrants, mostly from Sudan, cheered and danced on hearing the news. Malta's navy will ferry them ashore, but Malta will not let them stay.
France, Germany, Ireland, Luxembourg, Portugal and Romania will host them.
SOS Méditerranée, one of the French charities involved, had urged a resolution after two weeks at sea.
It said people were dying because the system for saving migrants from drowning had broken down.
It was the latest tense standoff involving charities and EU governments, as there is no binding EU agreement to relocate and house non-EU migrants.
People-smuggling gangs continue to make huge profits from African migrants desperate to reach Europe. Libya is a major transit hub, but it is chaotic and violence is rife. Many migrants then suffer dehydration and malnutrition on unseaworthy boats off Libya's coast.
SOS Méditerranée is running the rescue ship jointly with Médecins Sans Frontières (MSF).
France has agreed to accept 150 of the migrants and Portugal up to 35. The other allocations are not yet clear.
EU Migration Commissioner Dimitris Avramopoulos negotiated the deal with Malta and the other six EU countries.
On Tuesday, about 100 migrants stranded aboard another rescue ship, the Open Arms, disembarked on the Italian island of Lampedusa after 19 days at sea. They were only allowed ashore once five other EU countries had similarly agreed to host them.
Italy's Interior Minister Matteo Salvini calls the rescue ships taxis for people-smugglers and refuses to let them dock in Italy. His tough stance has contributed to the current turmoil in Italian politics, with the collapse of his coalition deal with populist Five Star.
In a tweet, Mr Salvini said: "As promised, we did not give the 356 migrants on the Ocean Viking permission to disembark in Italy. The safety of Italians comes first!"
He accuses EU partners of leaving Italy at risk of becoming "Europe's refugee camp".
The MSF coordinator aboard the Ocean Viking, Jay Berger, expressed relief that the "excruciating wait" was finally over.
AFP news agency reports that about two-thirds of the migrants are from Sudan, the others from West Africa. Almost 100 are aged under 18.