Spain allowed only two immigrants into Ceuta to be taken to the hospital while the rest were returned to Morocco.
At least 800 sub-Saharan African immigrants tried to cross into Spain's North African enclave of Ceuta from Morocco on Sunday by storming a border fence, though most were eventually turned away, the Spanish and Moroccan governments said.
Dozens of immigrants made it to the top of the six-metre barbed wire fence early on Sunday before being taken down by cranes, footage from local TV station Faro TV showed.
Spain said about 1,100 immigrants attempted the crossing. Only two were allowed into Ceuta to be taken to the hospital while the rest were returned to Morocco, the Spanish government said in a statement.
Five Spanish police and 50 from Morocco were injured, the government added after immigrants used rocks and metal bars to try and break through gates to access the fence and clashed with authorities.
Morocco's interior ministry reported that some 800 immigrants had tried to storm the enclave and that all had been arrested. It said 10 members of its security forces were seriously wounded.
"From now on those making such attempts will be presented before the competent judicial authorities who will decree their expulsion from the kingdom (of Morocco) or heavier penalties, according the gravity of the act," the ministry said in a statement.
Spain's two enclaves in Morocco, Ceuta and Melilla, are often used as entry points into Europe for African immigrants, who either climb over the border fences or try to swim along the coast.
Spain has drawn criticism from human rights groups for allowing some immigrants to be immediately returned to Morocco in such incidents. They argue that skipping the lengthier deportation procedures deprives people of the opportunity to claim asylum.
In early December more than 400 sub-Saharan African immigrants managed to force their way over the Ceuta border fence.
However, Libya has become a more common departure point for African immigrants, most of whom come from sub-Saharan countries and attempt the crossing to Italy by boat.
2016 was the deadliest year ever for immigrants in the Mediterranean, with almost 5,000 deaths, according to the International Organization for Migration.
Separately on Sunday, Spanish police said a Moroccan woman was arrested in Ceuta last week for trying to smuggle a 19-year-old immigrant from Gabon across the border with Spain, curled up inside a suitcase.