The rescue services were alerted to the presence of a "polyester boat with a group of illegal immigrants on board that was in difficulty off the Marchica lagoon, in Nador province," local authorities were quoted as saying by the official MAP news agency.
They arrived at the scene and recovered five bodies, all male, while managing to save seven of the passengers, including four women, who were taken to hospital in Nador for treatment.
Nador lies next to Melilla, a Spanish enclave bordering Morocco on the Mediterranean coast, one of the European Union's only two land borders with Africa, along with the other Spanish territory of Ceuta to the west.
Thousands of African immigrants attempt to reach Spain every year from northern Morocco, either via Melilla or by crossing the Strait of Gibraltar, often in overcrowded and makeshift boats.
In the early hours of Monday, more than 30 sub-Saharan migrants stormed the six-metre (20-foot) border fence at Melilla, five of whom managed to penetrate the territory, according to the Spanish authorities.
Last month, 11 African migrants, including three women and two children, died when their boat carrying 34 people capsized in Moroccan waters as it was heading for Spain.