Four sub-Saharan African migrants drowned on Wednesday after their boat capsized off the Moroccan coast, local media reported.
A further 34 people including five women were rescued during a joint operation by local police and rescue services, semi-official news agency MAP quoted local authorities as saying.
The inflatable boat had capsized off the northern city of Al-Hoceima, it said, adding the prosecutor's office had opened a probe "to clarify the circumstances of this attempt at illegal emigration".
Migrants often attempt to reach Europe via the Strait of Gibraltar, just 16 kilometres (10 miles) wide at its narrowest point, leaving from a point on the coast several hundred kilometres west of Al-Hoceima.
Attempts to reach mainland Spain by sea from Al-Hoceima, some 130 kilometres away, are less common.
Al-Hoceima lies halfway between the Spanish enclaves of Melilla and Ceuta on Morocco's northern coast.
Migrants also frequently try to cross into the two Spanish territories by swimming along the coast or climbing over complex razor-wire barriers.
Spanish and Moroccan authorities have been cooperating to prevent migrants crossing from the African side of the borders into the enclaves.
The International Organization for Migration (IOM) says 5,445 migrants arrived by sea in Spain between January and September, while 62 people died while making the crossing.