Valletta agreed to let the migrants disembark after Madrid sent a plane to take most of them to Spain.
From Spain, the migrants - most of them from Eritrea - will be taken to various European and African nations. Malta will accept three migrants.
Malta had refused to allow the ship to dock, saying they were rescued outside its territorial waters.
It had said the men, women and children on board were the responsibility of Spain or Libya's as since the boat was Spanish and the migrants were picked up in Libya's search-and-rescue zone.
The deal was confirmed by Maltese Foreign Minister Michael Frendo.
It followed days of intense negotiations between Malta, Spain and EU Justice Commissioner Franco Frattini. This was a clear signal of collaboration at European Union level," Mr Frendo told the AFP news agency.
"This was also a clear sign of sharing the burden in these very difficult circumstances," he said.
Under the deal, Malta will take three of the migrants, who needed treatment.
The rest would be flown to Spain and will be distributed between several European and African nations.
EU border guards
On Tuesday, Mr Frattini said the EU was about to launch a mission to patrol the Mediterranean near Malta, as he announced plans to set up a system for sending rapid reaction border guard teams to countries facing a migration crisis.
Since January almost 1,000 immigrants have landed on Malta, which has a total population of 400,000.
Malta says it has no more capacity to deal with illegal immigrants and has been pressing the EU to help.
Spain, meanwhile, is dealing with its own immigration crisis in the Canary Islands, with some 11,000 African migrants arriving on the island chain so far this year.