Officials said about 40 of the migrants managed to cross the fence separating the Spanish enclave of Melilla from Morocco on the Mediterranean coast, in the latest in a wave of such attempts.
The migrants stormed a section of the six-metre (20-foot) fence near the airport in Melilla on Tuesday morning, Spanish government officials there said in a statement.
"The immigrants were grouped in a grove of trees on the Moroccan side and carried out a massive coordinated assault which the Civil Guard managed to partially abort," the statement said.
"In their efforts to defend the border fence, five civil guards were injured, none of them seriously for the moment.
Authorities have reported hundreds of Africans trying to enter the territory over recent months, by sea or over the fence, prompting clashes that have left migrants and security forces injured.
On March 11, some 25 people were injured as they sought to breach the fence. A Moroccan human rights group said that one of them, a Cameroonian migrant of 30, died of his injuries in Morocco.
Melilla, home to around 80,000 people, has one of the European Union's two land borders with Africa, along with the other Spanish enclave of Ceuta to the west.
Spanish authorities have reported a surge in attempts to scale the fence since a crackdown on arrivals via Spain's Canary Islands and in the wake of violent unrest in northern Africa over recent years.
Rights groups say many migrants are camping in the wild on the Moroccan side waiting for a chance to cross.
Medical aid group Doctors Without Borders this year closed its projects in Morocco in protest at the treatment of migrants who are brought there by traffickers and allegedly abused by Spanish and Moroccan police.