The migrants had placed themselves in an “unlawful situation” by not seeking refuge through the correct channels, which means Spain could not be expected to offer them any protections, it said.
Two migrants, one from Mali and the other from Ivory Coast, approached the court after they sought to cross into Europe with a large group of others in the Spanish enclave of Melilla, which is surrounded by Moroccan territory.
The Spanish authorities had erected a triple-layer fence along the 13-kilometre (eight-mile) border.
In August 2014, hundreds of migrants charged the border, with about 100 making it over the first fence, and about 75 reaching the inner fence. A few landed on Spanish soil, where they were met by civil guard members who escorted them back to Moroccan territory.
They did not undergo an identification procedure nor were they asked to explain their personal circumstances, according to the court ruling.
“Today's judgement is very disappointing,' said Amnesty international spokeswoman Anna Shea.
“These two men were marched back to Morocco as soon as they entered Spain, with no chance to explain their circumstances, no chance to request asylum, and no chance to appeal their expulsion.
“That the court has today decided that Spain was within its rights to do this, because the men entered the country irregularly, is truly a blow for refugees and migrant rights,” she added.
'Placed themselves in jeopardy'
A spokesperson for Spain's Interior Ministry said the government would “respect” the decision.
In an initial judgment in 2017, the court held that Spain had violated a European protocol on the prohibition of collective expulsion, as well as the migrants' right to an effective remedy.
Spain challenged these findings to the court's Grand Chamber, a sort of appeals tribunal.
On Thursday, the chamber ruled that Spanish law had afforded the migrants “several possible means of seeking admission to the national territory”.
They could have applied for visas or sought asylum at the border.
“The Court considered that the applicants had in fact placed themselves in jeopardy by participating in the storming of the Melilla border fences on August 13, 2014, taking advantage of the group's large numbers and using force,” said the ruling.
It said they had not used existing procedures to gain lawful entry to Spanish territory.
“Consequently, the Court considered that the lack of individual removal decisions could be attributed to the fact that the applicants had not made use of the official entry procedures existing for that purpose, and that it had thus been a consequence of their own conduct,” it said.
As for the migrants' inability to appeal their removal, the court found that they themselves had been “required to abide by the rules for submitting such an appeal”.
As a result, Spain could not be held in violation.
Spain's two North African enclaves, Melilla and Ceuta, have the European Union's only land borders with Africa, from where many fleeing wars and poverty try to find their way to the old continent.