They were the latest in a flow of thousands of migrants trying to reach Melilla and the other Spanish north African territory of Ceuta, which together have Europe's only borders with Africa.
One group stormed the six-metre (20-foot), triple-layer fence around Melilla early on Tuesday morning and about 30 managed to climb over, Spanish government officials said in a statement.
Shortly afterwards another group charged at another part of the fence but were kept away by Moroccan security forces in coordination with Spanish police, the statement said.
The two groups totalled more than 600 migrants and several remained perched on top of the six-metre (20-foot) fence, at the foot of which Spanish police remained deployed.
No migrants nor police were known to have been hurt in the incident, the statement added — unlike during some earlier attempts this year.
The flow of migrants seeking to enter Europe via Ceuta and Melilla has intensified this year.
Some 16,000 migrants have tried to storm the Melilla fence and more than 2,000 have made it over, the Spanish government delegation there says.
There have been at least 65 attempts by groups of Africans to climb the fence this year, compared to a total of 38 in 2013.
Spanish authorities accuse human-traffickers for bringing the migrants to the border in the hope of reaching European soil.