“I have always told our Eastern European partners that it is correct to put questions to the courts,” Mr. Gabriel said. “But now we can also expect that all European partners will uphold the ruling and the carry out the decision without further delay.”

Hungary could be ordered to pay fines if it fails to take in its quota of 1,294 people, though enforcement might require the European Commission to bring its own court case.

Poland and the Czech Republic may face similar legal action, but Slovakia has accepted a small number of migrants relocated from Greece and has recently shown a greater willingness to find common ground with Brussels.

Peter Szijjarto, the Hungarian minister of foreign affairs and trade, called the ruling “outrageous and irresponsible” and said “the real battle is only just beginning,” insisting that no one would be relocated to Hungary against its wishes.

The quota system was introduced as part of an effort to relieve the burden on Greece and Italy, where migrants — many of them fleeing the war in Syria — reached the European Union in huge numbers in 2015 and 2016. But the bloc soon faced intense opposition from member states in Central and Eastern Europe that resented any obligation to accept a preset number of migrants arriving in Greece and Italy.

The program formally ends Sept. 26, but migrants arriving in European Union states until that date can qualify. According to estimates by European officials, about 22,000 people who have arrived, or who are expected to arrive in Greece and Italy before the September deadline, could be eligible for relocation. But only about half of them are likely to qualify for relocation to another member state, where then they could apply for asylum, officials said.