The bodies of the migrants, from sub-Saharan Africa, were spotted on Saturday by a Spanish ship, which alerted the rescue services of both countries, the spokeswoman said.
She added that “about 20” bodies were then recovered in Moroccan territorial waters. Moroccan authorities have yet to issue a figure.
Late on Saturday, a Spanish police patrol boat found one more body, which was taken to Melilla, a Spanish enclave bordering Morocco. Migrants are increasingly favouring the so-called western Mediterranean
route to reach Europe, which involves making the sea crossing between north Africa and southern mainland Spain.
According to the International Organization for Migration, Spain has been the second most popular point of entry for migrants coming to Europe so far this year, with 1,279 arrivals, after Italy with 4,256.
It said 243 people have died or are missing in the Mediterranean after trying to cross the Mediterranean this year, not including those recovered this weekend.
Spain was last year the third point of entry for Europe-bound migrants, after Italy and Greece. Arrivals by sea tripled in 2017 compared to the previous year, reaching a total of 22,900 migrants, according to the EU border