The man, who ran into the border post, was subsequently detained, Spanish Interior Minister Juan Ignacio Zoido said.
La @policia ha detenido a un hombre que atacó con un cuchillo a los agentes en el paso fronterizo de Beni Enzar, uno de ellos herido leve.— Juan Ignacio Zoido (@zoidoJI) July 25, 2017
According to local reports, the incident happened at 7.45 when the man rushed towards the border control at the Beni Azar gate, brandishing a knife.
Officers intercepted him and pulled him to the ground, with one recieving a "minor injury" to his hand.
The minister also posted a video of the attack which shows agents wrestling the man to the ground, after knocking him off balance with a section of road block.
Melilla and its sister city Ceuta are two Spanish territories located on Morocco's northern coast, and as such represent the only two land borders between Africa and the European Union.
They have been hit by unrest before as migrants desperate to reach Europe regularly storm the border between both territories or try and smuggle themselves in, but this is the first attack of this type.
Spain has been spared the type of jihadist 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 militants.