Spain has deployed troops after record numbers of migrants entered its north African enclave of Ceuta from Morocco.
Some 8,000 people have reached Ceuta in two days, Spanish officials say.
They say the migrants - who include about 1,500 minors - either swam around the border fences that jut out into the sea or walked across at low tide.
Spain's Prime Minister, Pedro Sánchez has vowed to restore order. Spain says around half of the migrants have been sent back to Morocco.
By Tuesday evening, footage of the beach showed that nearly all migrants had been cleared.
Mr Sánchez travelled to Ceuta and Melilla to deal with the crisis, which exacerbated diplomatic tensions with Morocco.
Morocco withdrew its ambassador for consultations after Spain's foreign minister told the envoy of her "disgust" at what had happened.
Most of the migrants are said to be from Morocco. The Spanish troops have been deployed to the beach to help border police at Ceuta's main entry point - Tarajal, on the enclave's south side.
Dramatic scenes were captured offshore as families waded through the water and officers from Spain's Guardia Civil went into the sea to rescue young children.
Interior Minister Fernando Grande-Marlaska said 200 troops, plus 200 extra police were going to assist Ceuta's normal 1,100-strong border force. The enclave has some 80,000 residents.
By evening reports said the number of people trying to enter by sea had decreased. Some migrants were voluntarily returning to Morocco while others could be seen being carried away by soldiers.
Spain's Ceuta and Melilla enclaves have become magnets for African migrants trying to reach Europe.
Also on Tuesday, Moroccan security forces at Fnideq, the adjacent town to Ceuta, fired tear gas to disperse a large crowd of migrants at the border fence, AFP news agency reported.
Mr Sánchez has received support from senior EU officials, with European Council President Charles Michel tweeting: "Spain's borders are the European Union's borders."
EU Commissioner for Home Affairs Ylva Johansson described the number of arrivals in Ceuta as "unprecedented" and "worrying", noting that "a big number of them [are] children".
At the other enclave, Melilla, 86 sub-Saharan Africans entered on Tuesday via its southern jetty.
Melilla has a formidable border fence, and several hundred more migrants were blocked by security forces, Spain's Efe news agency reports.
Spanish officials quoted by Efe said Moroccan guards had helped the Spanish forces in Melilla.
Spanish media said it was different in Ceuta, where Moroccan border guards stood by and watched as migrants took to the sea to try to reach the enclave.
Most of the migrants were said to be young men, but there were also several families. Many had used inflatable rings and rubber dinghies.
They started arriving in Ceuta in the early hours of Monday. At least one died during the crossing.
Last month, more than 100 migrants arrived at Ceuta's Tarajal entry point. Most were sent back, except about 30 minors whose ages were confirmed by medical tests.
Since the 17th Century Ceuta and Melilla have been under Spanish rule, though they are long claimed by Morocco. They have semi-autonomous status, like some regions of mainland Spain.
The influx comes amid renewed tension over Western Sahara, a territory occupied by Spain until 1975, when Morocco annexed it. Since then it has been disputed between Morocco and the indigenous Sahrawi people, led by the Polisario Front.
Souring of ties over Western Sahara
In April, Spain allowed Sahrawi leader Brahim Ghali, 73, to be treated in hospital for Covid-19, reportedly in Logroño. He leads the Polisario Front, fighting for Western Saharan sovereignty against the claims of Morocco. The Moroccan government responded angrily and warned Spain that harbouring Mr Ghali would bring "consequences".
Spanish Foreign Minister Arancha González said she was not aware of Morocco using the border issue to exert political pressure. But many see Monday's events, when Moroccan border guards appeared not to stop migrants from crossing, as evidence of a reprisal.
Such difficulties are likely to complicate the two neighbours' normally tight co-operation on the migrant issue. However, Spain says it has already repatriated about half of the migrants, following talks with Morocco.
The vast majority of those who reached Ceuta were Moroccan. Local police have clamped down on sub-Saharan migrants in northern Morocco in recent years, meaning they have sought other routes to Spain, such as across the Atlantic to the Canary Islands.
The EU border force Frontex reports that illegal migration to Spain's Canary Islands - off the Moroccan coast - has surged this year.
However, the overall number of undocumented migrants reaching Europe so far this year remains far below the levels seen in 2015-2016.
Read more on migrants reaching Spain: