A surge of tens of thousands of migrants into the Spanish North African exclave of Ceuta has plunged Spain into crisis and put Europe on alert, with 57 bodies recovered by evening as most victims drowned attempting to swim from Morocco.

Prime Minister Pedro Sánchez, visiting Ceuta, described the events as an "attack" and a "violation of Spain's territorial integrity." The government in Madrid has deployed the military to support border security in the exclave for the first time since 2021.

Citing the government, state broadcaster Radio Nacional reported on Friday that nearly 50,000 migrants had entered the exclave irregularly in recent days via the Strait of Gibraltar. Regional President Juan Jesús Vivas put the figure even higher, at 60,000.

At the same time, the Madrid government said tens of thousands of migrants were voluntarily returning to Morocco across the border causeway.

On Friday evening, Spanish Foreign Minister José Manuel Albares declared the crisis effectively over on X, writing that nearly all of those who had arrived in Ceuta had since returned to Morocco.

Ceuta has a population of fewer than 85,000 and covers just 18.5 square kilometres. Thousands of migrants spent Thursday night on the streets of Ceuta.

State broadcaster RTVE reported that the CETI reception centre had been overcrowded since Wednesday, housing more than 1,000 people against a capacity of 512.

The government did not act on a request by regional President Vivas to declare a state of emergency. However, after ordering the military deployment on Friday, Sánchez travelled to the exclave and announced further measures. Floating buoys are to be installed along the border causeway between the Moroccan coast and the Ceuta beach of Tarajal to create a "physical barrier."

Procedures are also to be accelerated to facilitate the return of migrants who entered irregularly. "The departure of many migrants is already under way," Sánchez said, praising the cooperation with Morocco.

The events have triggered fierce political debate across Europe, coming shortly after the hard-won EU asylum reform. Conservative Spanish newspapers including ABC and La Razón spoke of an "invasion." Opposition leader Alberto Núñez Feijóo criticized Sánchez, saying the government must "not look away" because Spain is "in a national security crisis."

On Thursday evening, many Ceuta residents demonstrated against the influx, accusing the central government of having lost control. Numerous shops remained closed amid fears of looting. RTVE quoted residents as saying: "We hope they take them away soon," and: "Many people are afraid to leave their homes."

The European Commission said there had so far been no onward movement towards the European continent, according to a senior official speaking to journalists.

In the past, Ceuta's regional government has asked Madrid to transfer unaccompanied minors to the Spanish mainland due to overstretched reception capacity. It was initially unclear whether Spanish authorities would move people to the mainland.

European Commission President Ursula von der Leyen said the images from Ceuta were unacceptable. She called for tough action against smuggling networks and swift returns, saying Brussels was working on support for Spain, including through EU border agency Frontex.

France tightened checks at its border with Spain in response to the migrant influx. Italy's right-wing government in Rome said it had temporarily suspended the Schengen agreement on free movement with Spain.

Finnish Interior Minister Mari Rantanen wrote on X that Spain had failed to protect the Schengen area's external border.

German Chancellor Friedrich Merz called on the Spanish government to bring the situation under control as quickly as possible. "Protecting the European Union's external borders is crucial to combating illegal migration," the conservative politician said.

Sánchez later hit back at the critics, calling on EU member states to respect European treaties.

"Solidarity and compassion are voluntary. Respect for European treaties and the data is not," Sánchez wrote on the platform X on Friday evening. He said it was time to continue building a strong and united Europe - not to divide it.

Sánchez cited data from EU border agency Frontex showing that Spain is not the main entry point for irregular migration into the EU and has recorded significantly lower numbers since 2021 than routes such as the central Mediterranean route via Italy, the Western Balkans route and the route via Greece.

Several explanations have been put forward for the unprecedented surge. Poverty and a lack of prospects for young Moroccans are cited as underlying factors.

One possible trigger is a ruling by Spain's Supreme Court in Madrid stating that the immediate return of migrants is only permissible if they crossed a fence or other barrier during an irregular border crossing. Those who enter Spain via an unfortified section such as the sea must be assessed on a case-by-case basis - a factor also behind Madrid's decision to install buoys at sea.

However, the newspaper El País and other observers stress that this does not fully explain the crisis.

It appears that Moroccan authorities suddenly began tolerating departures towards Ceuta, reviving suspicions that Rabat may be using migration as a political lever against Spain - as it did during the 2021 Ceuta crisis - to press its claim to Ceuta and the other Spanish enclave of Melilla, or out of anger over a visit by Sánchez to Algeria last week. Algeria is at odds with Morocco over the Western Sahara conflict.

There was no immediate official reaction from Rabat. However, Morocco's ambassador to Madrid, Karima Benyaich, stressed that her country wanted the return of its nationals and favoured "legal, orderly and safe migration."

The images from Ceuta are a reminder of May 2021, when more than 8,000 people stormed the enclave within 36 hours.