Spanish police shot tear gas toward a group of people gathering near the border between Morocco and Spain's enclave of Ceuta on Wednesday, to prevent them from crossing into the European country.
It comes after more than 8,000 people crossed into Spanish territory from Morocco during the previous two days, with many risking their lives by swimming around a breakwater to reach a beach on the European side.
Exhausted by either scaling a double-wide border fence or swimming in the Mediterranean, some migrants spent the night in an overcrowded warehouse under the gaze of the Spanish police.
Spain's Interior Ministry said that it had returned roughly half of the migrants, but there were many unaccompanied minors, which Spanish law says must be taken into care by authorities.
Many shopkeepers kept their stores shuttered, while local schools in the city of 85,000 reported that attendance dropped by 60%.
While Morocco was tightening controls of a border after two days of allowing crossings, Spanish authorities increased their criticism of counterparts in Rabat.
Prime Minister Pedro Sanchez told Spain's Parliament, a day after he visited Ceuta, that the lack of border control was an act of defiance and disrespected not Spain but the European Union.
Foreign Minister Arancha Gonzalez Laya also increased her tone on Wednesday, saying for the first time in public that Spain believes Morocco loosened its border control to retaliate for Spain having given medical assistance to the head of the Western Sahara liberation movement, a disputed territory to Morocco's south.