Pedro Sánchez, the Spanish prime minister, promised to restore order in the Spanish enclave of Ceuta after around 8,000 people swam and waded across the border from Morocco.
The record influx of mostly Moroccan migrants, including 1,500 minors, started on Monday and continued until midday on Tuesday, prompting Madrid to deploy the army to bring the situation under control.
Entire families tried to circumvent breakwaters in the sea to enter the Spanish territory illegally, resulting in the death of at least one man.
Fernando Grande-Marlaska, Spain's interior minister, said 4,000 migrants had been repatriated to Morocco, adding that 200 extra police officers had been sent from the mainland to help round up and deport the migrants.
"This sudden arrival of irregular migrants is a serious crisis for Spain and Europe," Mr Sánchez said in a televised address to the nation before he set off to travel to the enclave.
"I want to tell all Spaniards, especially those in Ceuta and Melilla, that we will re-establish order in your city and at our borders with the utmost speed. We will act firmly to ensure your safety."
The surge in crossings occurred after Rabat scaled back its border policing, seen by analysts as an attempt to put pressure on Spain over a dispute linked to Western Sahara.
Morocco has claimed the territory on its southern border ever since the Spanish abandoned the one-time colony in 1975.
That has long been rejected by the Polisario Front, an organisation fighting for Western Sahara's independence. Spain takes the UN position, which is that there should be a negotiated solution and a referendum on the issue.
The Ceuta border crisis comes after Spain provided treated Brahim Ghali, leader of the Polisario Front, for Covid. Arancha González Laya, the Spanish foreign minister, insisted there was no connection between the two events.
But Karima Benyaich, Morocco's ambassador in Spain, said: "There are acts that have consequences and they have to be accepted."
His comments were widely interpreted as a reference to Mr Ghali's medical treatment in Spain.
On Tuesday, Ceuta's regional leader said the influx of migrants was the result of Moroccan border authorities suddenly turning a blind eye.
"This is happening because of the absolute passivity of the Moroccan authorities," Juan Jesús Vivas told Spanish television. Analysts have said the withdrawal of border police is linked to Western Sahara dispute.
Rafael Calduch, a professor of international law at Madrid's Complutense University, told Reuters: "The passivity of Moroccan police is a direct consequence of the hospitalisation of Ghali in Spain."
Irene Fernández-Molina, an international relations professor at Exeter university, said Morocco saw Mr Ghali's treatment as a "very hostile decision" as he was "the leader of their main enemy".
As Mr Sánchez flew to the enclave on Tuesday afternoon, he reminded Morocco that "cooperation must be based on respect for shared borders".
It came as Ms Laya summoned Morocco's ambassador, "to express our discontent and rejection of the mass influx of Moroccan migrants to Ceuta and remind them border control must be their joint responsibility".