More than a thousand African migrants have stormed a border fence in Spain's North African coastal enclave of Melilla, with many managing to scale the barrier and cross, Morocco's government has said.

Dozens of others were beaten back by police on both sides of the frontier on Wednesday. 

The Moroccan government said 1,500 immigrants rushed through the fence at five different points, ignoring warnings to stop and hurling stones at security forces.

At least 500 people made it across the border, shouting with joy as they dashed through the streets to an official accommodation centre. The centre now houses about 2,400 people, an official there said. 

Morocco's Interior Ministry said four officers were hurt and 10 migrants were treated at a hospital. A Spanish official said several of the migrants who made it across were also treated for injuries. The officials commented on condition of anonymity because of government rules preventing staff from being identified, the Associated Press reported.

Two men perched atop lamp posts. Spanish police used a crane to drag one down, while the other eventually came down on his own. They were both handed over to Morocco, AP reported.

Spain's North African city enclaves of Melilla and Ceuta are regularly targeted by large groups of African immigrants living illegally in Morocco to try to cross into Europe in search of a better life.

Some have tried to cross by sea. On Wednesday authorities in the Moroccan port of Tangiers said the bodies of two migrants were pulled from the water and eight migrants rescued after their boat sank the previous night. A woman remained missing.

Spain and Morocco have stepped up border vigilance since 15 migrants drowned in February trying to enter Ceuta.