Italy's new populist government – which under the leadership of nationalist Interior Minister Matteo Salvini has taken a hardline stance on immigration – came under fire from its EU neighbours after refusing to let a foreign charity boat carrying 629 migrants dock in Italy.

The migrants aboard the Aquarius, run by French charity SOS Mediterranean, were stuck on the overloaded ship for over 30 hours as Italy and Malta bickered over who should take them.

Spain then stepped in and offered the port of Valencia, where the Aquarius is heading.

"As a European I feel ashamed that there is a boat roaming around the sea," UN High Commissioner for Refugees Filippo Grandi told reporters in Geneva.

"I think that closing ports, whoever does it, threatens rescue at sea, as we have seen in the case of the Aquarius, and therefore is not the right solution," he said.

But he stressed that "the reason why Italy said it had closed ports is something we need to listen to".

Grandi, an Italian national himself, pointed out that Italy had taken the brunt of the massive numbers of migrants crossing the Mediterranean from Libya towards Europe in recent years.

Salvini has repeatedly vowed to stem the flow of migrants to Italy, which has seen more than 700,000 arrive on its shores since 2013.

Under EU rules, migrants must apply for asylum in the European country where they first arrive.

That has put pressure on Italy and Greece, the entry points for hundreds of thousands of people fleeing war and poverty.

"I think that Italy is right to say that they cannot all go to Italy," Grandi said.

EU states adopted a temporary quota system in 2015 as a form of solidarity with Greece and Italy during the height of Europe's migrant crisis.

Under that plan, 160,000 refugees were supposed to be relocated to other members of the bloc, but so far only around 30,000 relocations have taken place.

"The relocation program for Greece [and] Italy, sorry, was a failure," Grandi said.

"Unless Europe goes back to this notion of sharing the burden, we will not get out of this situation," he said.

The UN refugee chief said there was an urgent need to clarify states' responsibility when it comes to allowing disembarkation in the cases of large-scale rescues.

"We have to have predictable arrangements, so that rescue boats know where to go," Grandi said, warning that "otherwise, this [could be] a disincentive to rescue, [and] people may lose their lives if this doesn't happen."

Unless there is some clear division of responsibilities, "you will have more Aquarius' happening in the next few weeks, and this is profoundly shameful," he said.