The crowd charged around dawn in the latest in a string of desperate attempts to breach the fence, one of Europe's two land borders with Africa.
Hundreds of migrants have scrambled across in recent months, but this time Spanish authorities said that dense anti-climb fencing recently installed on the border helped stop them.
"Anti-climbing mesh and the collaboration of the Moroccan forces with the (Spanish) civil guard managed to stop an attempt to cross by about a thousand migrants," the Spanish government delegation in Melilla said in a statement.
"None of the immigrants managed to reach the city," it added.
Moroccan forces saw off many of the migrants and those who managed to get past them to the fence failed to climb into Melilla partly due to the new type of mesh, it said.
Melilla is one of two Spanish-held cities on the northern coast of Africa, along with Ceuta to the east. The number of Africans trying to cross into them has surged over recent months.
On May 28 about 500 African migrants climbed over the seven-metre (23-foot) triple-layer border fence into Melilla — one of the biggest breaches in nearly a decade.
Spain has asked for more money from the European Union to secure its borders. Officials say the immigrant reception centre in Melilla is highly overcrowded.
It was built to house 480 immigrants but it was holding about 2,000 even before the last wave of entrants, officials said last month.