Dramatic CCTV footage shows policemen overpowering a knife-wielding man who attacked officers on the Spanish-Moroccan border.
It is understood he was shouting "Allahu akbar" or "God is greatest" as well as other Arabic chants at the time of his rampage, according to local reports.
The video shows police carrying riot shields and batons surrounding the attacker, who is wearing a blue top and cream shorts.
As one group distracts him, an officer picks up a traffic barrier and throws it at the assailant's head, sending him crashing to the ground.
The remaining policemen then swarm around the downed attacker and promptly arrest him.
Spain's interior minister Juan Ignacio Zoido confirmed the attack in which one of the officers was injured.
He said: "The police have arrested a man who attacked with a knife agents at the border crossing of Beni Ansar."
The man, thought to be Moroccan, was detained near Melilla and detectives are now investigating.
Riot police swooped to arrest him after one of their colleagues was stabbed in the hand, according to El Pais.
The attack occurred at 7.45am when the knifeman ran though traffic bound for Beni Ansar near the crossing.
He ran towards the guarded fence and started threatening officers, injuring one, and was then brought down.
Ceuta and Melilla: Spain's two enclaves in Morocco
The Spanish outposts of Ceuta and Melilla in northern Morocco, which have the European Union's only land borders with Africa, are tempting launch pads for clandestine immigration to continental Europe.
Ceuta, a former Roman colony of 85,000 people, measures about 18 square kilometres and lies just across the Strait of Gibraltar from Spain.
It was captured by the Arabs and the Portuguese and has been under Spanish sovereignty since 1640.
Melilla, measuring about 200 square kilometres, is perched on the eastern edge of Morocco's Mediterranean coast, and has been under Spanish control since 1497.
It has a diverse population of about 85,000, of which around half are Muslims, and thousands of Moroccans go there to work and shop every day.
Thousands of migrants have attempted to cross the 12km frontier between Melilla and Morocco, or the 8km border at Ceuta, by climbing the border fences, swimming along the coast or hiding in vehicles.
More than 350 migrants stormed the six-metre-high double fence at Ceuta on Monday, just days after nearly 500 migrants made it over, one of the biggest entries since the barrier was reinforced in 2005.
The two territories are protected by fences fortified with barbed wire, video cameras and watchtowers. Migrants have died or been injured trying to breach the barriers.
On January 1 more than 1000 migrants from sub-Saharan Africa tried to scale the fence at Ceuta during a violent assault in which one officer lost an eye.
Both exclaves were conquered as part of a strategy by Catholic kings of establishing advance posts of Christendom on the African continent following the expulsion of Moors and Jews from Spain in 1492.
Claimed by Morocco, the two cities have long been a flashpoint in diplomatic relations between Madrid and Rabat, with Madrid insisting that both are integral parts of Spain.
Both port cities have developed as military and trade centres linking Africa to Europe, and since the 1990s have enjoyed a status similar to other autonomous areas such as the Basque region and Catalonia.