The man is in custody, Spanish Interior Minister Juan Ignacio Zoido tweeted, without indicating whether the incident was an attempted terror attack.
Zoido posted security camera footage of the drama -- which took place on Spanish soil -- showing the man in a blue top walking slowly through the border post holding a knife, police closing in on him.
One of the officers hurled a portable road barricade at him, throwing him to the ground as other agents pounced to remove the knife.
"A man entered the border post and once inside, pulled out a large knife and confronted (police) shouting 'Allahu Akbar' (God is Greater), slightly injuring a policeman," Irene Flores, spokeswoman for the central government's representative office in Melilla, told AFP.
Flores said an initial investigation indicated the man was Moroccan, but this has yet to be confirmed.
A police spokesman had earlier said the man ran into the border post, but that is not visible on the footage.
Melilla and its sister Spanish city Ceuta, both Spanish territories on Morocco's northern coast, are the only two land borders between Africa and the European Union.
Many Moroccans live there or go there daily to buy tax-free products.
They are also a strong draw for migrants desperate to reach Europe, many of them from sub-Saharan Africa.
These regularly storm the border fences or try and smuggle themselves in.
The Melilla border has been hit by three car-ramming incidents this year, with people driving vehicles with migrants hidden inside into the border post at high speed.
Spain has so far been spared the kind of extremist violence that has occurred in nearby France, Belgium and Germany.
But it was hit by what is still Europe's deadliest jihadist attack in March 2004, when bombs exploded on commuter trains in Madrid, killing 191 people in an attack claimed by Al Qaeda-inspired extremists.
Since 2016, Spain has emerged as a potential target for jihadists, with extremist websites mentioning it for historical reasons, since much of its territory was once under Muslim rule.
Third tourism destination
Generally, authorities in Spain -- the world's third largest tourism destination -- remain discreet on the terror threat.
But they publicise every arrest of alleged jihadists, most of them detained for propaganda, recruitment for extremist groups or "glorifying terrorism."
According to the interior ministry, more than 180 "jihadist terrorists" have been arrested since June 2015 when Spain raised the terror alert level to four out of a maximum of five, in domestic and foreign operations.
In Ceuta and Melilla, where poverty and unemployment are rife, such arrests are frequent.
The last in Melilla dates back to June 23 when a man suspected of having tried to recruit fighters for the ISIS group (IS) was detained.
"Radicalisation in Spain isn't uniform over all the national territory but appears to be concentrated around clusters or pockets of radicalisation," said Clara Garcia Calvo from the Real Instituto Elcano think tank, who researches global extremism.
She told AFP that these "clusters" were in Madrid, Barcelona, Ceuta and Melilla.
Get the latest election news, live updates and election schedule for Lok Sabha Elections 2019 on ndtv.com/elections. Like us on Facebook or follow us on Twitter and Instagram for updates from each of the 543 parliamentary seats for the 2019 Indian general elections.