"It is our obligation to help to avoid a humanitarian catastrophe and to offer a safe port to these people," Prime Minister Pedro Sanchez's office said in a statement, adding that the boat, the Aquarius, would be allowed to dock in Valencia.
The migrants were rescued in six separate night-time operations in the central Mediterranean on Saturday. The French organisation SOS Méditerranée said that 123 unaccompanied minors, 11 small children and seven pregnant women were among those brought on board.
SOS Méditerranée director general Sophie Beau said Spain's offer "is encouraging and shows that some states are sensitive to the humanitarian emergency".
She warned that the ship would have to travel some 1,300 kilometres to reach the Spanish coast and that by Tuesday "there will be no more food [on board] apart from energy biscuits". Malta's Prime Minister Joseph Muscat said that Malta would send fresh supplies to the ship.
Italy and Malta thanked Spain for stepping up, but maintained their dispute over who was responsible.
"We asked for a gesture of solidarity from Europe and this gesture has been made," Italy's Prime Minister Giuseppe Conte said.
Muscat tweeted his thanks to Sanchez "for agreeing to accept the Aquarius after Italy violated international law and caused an impasse." He added: "It will be necessary to sit down and discuss how to prevent this kind of thing from happening in the future. This is a European issue."
Photo: Carmelo Lenzo/AFP
The crisis arose after both Malta and Italy refused the Aquarius permission to dock, with each accusing the other of failing to meet its international obligations.
As the United Nations and European Union pleaded for a swift resolution to the standoff, Italy's new government stuck firm to its hard line on immigration.
"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," Interior Minister Matteo Salvini wrote on Twitter, followed by the hashtag "#closethedoors".
After Spain announced it would accept the boat, Salvini declared "VICTORY" via Facebook, while Labour Minister Luigi Di Maio – who along with Salvini also serves as Italy's join deputy prime minister – called it a "turning point" that proved Italy is "no longer alone".
The new cabinet has pledged to take a stand against EU migration policies that they say leave Italy and its Mediterranean neighbours bearing the brunt of one of the biggest migration crises in a century.
EU rules mean migrants must apply for asylum in the European country where they first arrive, putting pressure on Italy and Greece, the entry points for hundreds of thousands of people fleeing war and poverty in the Middle East, Africa and Asia since 2015.
EU leaders in December had set an end-of-June deadline for an overhaul of rules to create a permanent mechanism to deal with migrants in the event of a new emergency.
Prime Minister Conte, who on Monday asked the head of Nato for more assistance on the crisis in the Med, has said that migration is a "litmus test" for Italy's relations with the EU, promising that the new government will push for asylum seekers to be automatically and obligatorily resettled across member states.
Photo: Louisa Gouliamaki/AFP