The early-morning surge of 500 people "ended with 140 immigrants of sub-Saharan origin entering national territory", according to a statement from Spanish officials in the city.
Some who did not make it to Spanish soil were caught in the border zone, where they stayed for a number of hours shouting "Freedom!", before being arrested by Moroccan police, local media said.
Six migrants and three police officers were injured, according to the statement, with some of those trying to jump the fence setting fire to their clothing "to throw at the agents".
Another group of around 200 people made a further attempt to cross the border a few hours later but they were stopped by border guards.
Moroccan authorities said 750 illegal immigrants had tried to make the crossing in two groups.
They ignored warnings and hurled stones at Moroccan security forces, slightly wounding two of them, authorities in the northern province of Nador said, quoted by Morocco's MAP news agency.
They said a total of 669 people were arrested and 20 others taken to hospital in Nador with injuries from the barbed wire fence.
Melilla, on Morocco's northern coast, provides one of only two land borders between the European Union and Africa, along with another Spanish territory, Ceuta, to the west.
Both have seen a flood of illegal border crossings by African migrants seeking a better life in Europe.
In March, 1,000 people tried to jump the triple-layer fence separating the city from Morocco, with half of them getting through — the biggest such crossing in nearly a decade.
In February, 15 migrants are believe to have drowned in Moroccan waters while trying to swim to Ceuta from a nearby beach.
Rights groups and witnesses accused Spanish security forces of firing rubber bullets at the migrants in the water, although the government denied its forces had targeted the migrants directly.
The territories, which sit across the Mediterranean from Spain, are a draw for migrants who do not want to make the treacherous journey to Europe by water.
Depending on treaties with the immigrants' countries of origin, some migrants who make it to Spanish territory are repatriated.
But the latest influx is putting pressure on Spain with a temporary immigration centre in Melilla, designed for 480 people, now holding 1,900 — many of them in tents.
Spanish Prime Minister Mariano Rajoy has urged the European Union to provide more money and resources to help manage the border.
Spain is also boosting security. In late April, the government agreed to spend €2.1 million ($2.9 million) to try to stem illegal border crossings.