Spain's prime minister has said the country will take in a rescue ship stranded in the Mediterranean, to help avoid a humanitarian disaster.
Pedro Sánchez said he would give "safe harbour" to the Aquarius and the 629 people on board, after Italy and Malta both refused to let the ship dock.
The UN refugee agency and the EU had both called for a swift end to the stand-off between the two countries.
Mr Sánchez, who took office a week ago, said the ship would dock in Valencia.
The migrants aboard the Aquarius were picked up from inflatable boats off the coast of Libya at the weekend, in six different rescue operations, according to the NGO SOS Méditerranée.
"It is our duty to help avoid a humanitarian catastrophe and offer a safe port to these people, to comply with our human rights obligations," Mr Sanchez's office said.
The Council of Europe welcomed Spain's move, with the organisation's Commissioner for Human Rights, Dunja Mijatovic, tweeting: "Saving lives at sea is an obligation that states must always uphold."
Malta's Prime Minister Joseph Muscat tweeted his gratitude to Spain, saying Italy had broken international rules and caused a standoff.
He said Malta would be sending fresh supplies to the vessel, adding: "We will have to sit down and discuss how to prevent this from happening again. This is a European issue."
Spain's decision to take in the ship was hailed by Italian Interior Minister Matteo Salvini as a "victory" for his government's hardline immigration policy.
Who is on board the ship?
Those saved include 123 unaccompanied minors, 11 younger children and seven pregnant women, SOS Méditerranée said.
The minors are aged between 13 and 17 and come from Eritrea, Ghana, Nigeria and Sudan, according to a journalist on the ship, Anelise Borges.
"Most of them are sleeping outside. They are obviously exhausted, they have been exposed to the elements, they have been at sea for 20 to 30 hours prior to their rescue," she told the BBC.
"They are fragile and we have yet to learn what's going to happen to them," she added.
Why did Italy reject the ship?
Mr Salvini refused to let the ship in, saying: "Saving lives is a duty, turning Italy into a huge refugee camp is not."
"Italy is done bending over backwards and obeying - this time THERE IS SOMEONE WHO SAYS NO," he wrote on Twitter, with the hashtag #closethedoors.
Mr Salvini, leader of the right-wing League party, promised during Italy's recent general election to take a tough stance against migration.
He said Malta should accept the Aquarius, but it refused, arguing that it falls under Italy's jurisdiction.
Italy is the main entry point for migrants crossing from North Africa to Europe.
Mr Salvini has previously said he is considering action against organisations that rescue migrants at sea. He has accused them of being in cahoots with people-smugglers.
On Sunday, he said that Italy was saying "no to human trafficking, no to the business of illegal immigration".
"Malta takes in nobody," he added. "France pushes people back at the border, Spain defends its frontier with weapons."
SOS Méditerranée said late on Sunday that the Aquarius had been instructed by the Italian Maritime Rescue Co-ordination Centre to stand by in its current position, 35 nautical miles (65km) from Italy and 27 nautical miles from Malta.
Despite Mr Salvini's stance, the mayors of Taranto and Naples had both offered to welcome the migrants, with Taranto's Rinaldo Melucci saying the Italian port city was "ready to embrace every life in danger".
Naples mayor Luigi de Magistris tweeted that "if a minister without a heart leaves pregnant women, children, old people, human beings to die, the port of Naples is ready to welcome them".
A smart move by Spain's new prime minister
By Guy Hedgecoe, BBC News, Madrid
While Spain's willingness to take in the Aquarius has surprised many observers, for the new Spanish government it does make political sense for several reasons.
The move solves a potentially thorny problem for the European Union, bolstering Pedro Sánchez's hopes of becoming an influential leader within the bloc.
The new prime minister appears keen to distance himself from the legacy of his predecessor, Mariano Rajoy, whose international impact was discreet and who only took in a fraction of the refugees agreed upon in a 2015 accord with the EU.
In addition, this gesture is likely to impress left-leaning parties such as Podemos which were instrumental in voting the Socialist in to office on 1 June, and whose support he still needs.
What is the law on accepting ships?
Rules on disembarking and assisting rescue ships such as Aquarius are governed by international law.
The International Convention for the Safety of Life at Sea dictates that any ship learning of distress at sea must assist regardless of the circumstances.
It says that the country responsible for operations in that area has primary responsibility for taking them from the ship.
It also clearly states that the relevant government "shall arrange for such disembarkation to be effected as soon as reasonably practicable".
A big question for Spain: What happens to the next ship?
By Kevin Connolly, Europe Correspondent, BBC News
The European Union wrote its rules about how migrants should be handled in the 1990s when no-one could have imagined the collapse of Libya would create huge flows of desperate people heading across the Mediterranean from sub-Saharan Africa and the Middle East.
The rules say migrants are the responsibility of the first member state where they land - an overwhelming problem in countries like Greece and Italy where the election of a populist government is at least in part a response to the pressure. So the migrants are starting to shape the politics of Southern Europe.
And when Italy's Trumpish Deputy Prime Minister Matteo Salvini proclaims "victory", he's telling his voters that the promise of a tougher line on immigration is real.
He's challenging the EU to find a proper solution too, based on forcing other member states to accept quotas of migrants - something it's failed to do so far. And he's incidentally created a big question for Spain. Will its offer to the Aquarius be extended to further ships in the future?