Besides the six killed, 30 were injured during the attempt on Thursday, a Moroccan Interior Ministry source said.
"Confronted with the extraordinary violence of the attackers, who were driven by the energy of despair, the police legitimately defended their surveillance posts in front of the barrier and six immigrants died," the source said.
"Some were killed by shots fired by the police and others were killed trampled by their fellows," the source added. The Spanish authorities in Melilla said they were not aware of any fatalities.
Aljazeera reported that Moroccan and Spanish security forces had thwarted nearly 1000 African immigrants from crossing over before dawn's raid on Thursday.
Spanish police arrested 290 of the immigrants who had tried to climb the border fence in the early hours of Thursday.
A similar assault on the border fencing in the twin Spanish enclave of Ceuta last week saw five immigrants die amid bitter controversy on whether Spanish or Moroccan police had fired on them.
Two of the dead who fell on the Spanish side of the border bore bullet wounds which Spain insisted had come from the Moroccan side.
Spanish soldiers patrol Ceuta,
the scene of five Africans' death
The death toll for this summer's assaults on Ceuta and Melilla now stands at a combined 14.
According to a prefecture source in the Moroccan city of Nador, 12km south of Melilla, "some 500 illegal sub-Saharans tried to climb the fence at the Rostrogordo crossing, but failed following a large-scale and swift intervention by Moroccan security forces".
The source added the police had information an assault was imminent and swooped when it began around 7am local time.
"Moroccan and Spanish forces managed to stop them with considerable anti-riot gear," Spain's Cadena Ser radio had earlier reported.
Spain wants Morocco to accept
repatriated illegal immigrants
Spanish Interior Minister Jose Antonio Alonso meanwhile told legislators that the shots which had hit the immigrants who fell on the Spanish side of the fencing at Ceuta a week ago "do not correspond to Spanish Civil Guard-issue ammunition" as he appeared to pin the blame on Moroccan security forces.
Alonso added the Spanish "did not use live ammunition" in trying to repel the immigrants at Ceuta on 29 September and was "firmly convinced" that those who died had not died at Spanish hands.
Moroccan sources have insisted their troops were not authorised to fire and that they did not do so.
With several hundred immigrants having made it across the double-layer barbed wire fencing in previous assaults, Madrid said it is seeking Rabat's agreement to reactivate a 1992 accord on control of migration, which would see illegals returned to Moroccan soil.
Alonso said a first group of 70 were already set to be taken back immediately to Morocco by boat.