Morocco´s interior ministry said the operation Tuesday evacuated a forest "where migrants live in very precarious conditions".
"Similar operations will be conducted systematically to clear all the places where migrants who plan to organise irregular immigration attempts are squatting," it added.
Rights groups said around 1,250 mostly sub-Saharan migrants were affected in a "total sweep" of illegal foreigners in the area.
The ministry said the migrants were not harmed and that the operation was part of its fight against "smuggling and trafficking networks".
Authorities did not say where the migrants were taken.
According to rights groups they were transported in about 20 buses.
At least 35 people managed to cross illegally into Melilla on Tuesday after hundreds stormed the border fence.
Thousands of migrants have tried to reach Melilla or the other Spanish territory of Ceuta, which together make up Europe's only borders with Africa.
Rights groups and the UN refugee agency have accused Spanish forces of using violence towards migrants trying to climb the border fences.
The flow of migrants seeking to enter Europe via Melilla has intensified, with more than 65 attempts to climb the fence in 2014, compared with 38 in 2013, Spain says.
Around 16,000 migrants tried to storm the Melilla fence last year and nearly 5,000 made it over. In 2013 roughly 3,000 managed to cross.