The deal was announced after the migrants on the Alan Kurdi ship, rescued on Wednesday off Libya, had been barred from landing in Italy.
Italy's far-right Interior Minister Matteo Salvini has taken a hard line against migrants entering the country.
The migrants will arrive in the Maltese capital Valletta later Sunday, the Maltese government said late Saturday.
The German government and the European Commission made arrangements for the migrants to be shared among several EU countries, it said.
None of the 40 migrants will remain in Malta and the Alan Kurdi -- named after a Syrian toddler who drowned in the Mediterranean Sea -- will stay in international waters as they disembark.
The government gave no indication which countries had agreed to take them in.
But Portugal said on Saturday that it was prepared to accept five of those on board, and that France, Germany and Luxembourg had offered to take others.
On Thursday, Salvini said that the German government had told the European Commission that unless the 40 migrants onboard the Alan Kurdi were allowed to disembark in Italy it would not take in a group of 30 migrants it had already promised to accept.
"This is real blackmail," said Salvini. "It confirms that other European countries consider Italy as their refugee camp, but things have changed and we no longer accept orders and invasions."
During its last rotation off Libya in early July, the Alan Kurdi rescued 109 migrants and disembarked them in Malta.
Meanwhile another humanitarian ship, Open Arms, was looking for a port to disembark 121 migrants.
The Spanish NGO Proactiva Open Arms had rescued 55 migrants off Libya on Thursday, and then took on a second group of 69 the next day.
Two pregnant women and the sister of one of them were allowed to disembark in Italy for medical reasons, while the others remain on board the ship which is barred from entering Italian territorial waters.