Poland backed the Hungarian and Slovak complaint.
Beata Szydlo, the Polish prime minister, said: 'We expected this decision.
In March 2016, the European Union reached a deal with Turkey to send back asylum seekers who take clandestine routes to Greece from Turkey.
The case has highlighted a deep divide in the European Union as it has fumbled for a joint solution to the mass arrivals that have strained resources, roiled politics, and prompted outcries from human rights defenders who warn that the bloc risks violating worldwide law on the treatment of refugees.
EU Migration Commissioner Dimitris Avramopoulos said on Wednesday that arrangement "continues to work and deliver results", the AP reports. The arrival of refugees, who are predominantly Muslim in their faith has created socio-political tensions in the block and given to the rise of populism in Europe and anti-immigrant parties.
The EU on Wednesday won a high-level legal battle against eastern European countries that have refused to admit thousands of asylum seekers based on mandatory quotas for the bloc's member states.
Slovakia is not included in the legal action.
"The existence of those various adjustment mechanisms shows that the relocation mechanism for which the contested decision provides, taken as a whole, enables account to be taken, in a proportionate manner, of the particular situation of each Member State in this regard", the ruling states. But he also said his country's critical stance on the quota system and the migrants "has not changed at all". Of those, 19,200 were transferred from Greece and 8,212 from Italy. All four countries also say migrants will disrupt their societies and the EU's focus should be on protecting its external borders.
Hungary's government, led by prime minister Viktor Orban, is facing elections next spring, which could entail more harsh rhetoric against Brussels and migration quotas during the campaign.
Hungarian Minister of Foreign Affairs and Trade Peter Szijjarto, right, and Hungarian Justice Minister Laszlo Trocsanyi hold a joint news conference after the decision of the Court of Justice of Luxembourg about the European Union migrant quotas at the Parliament in Budapest, Hungary, on September 6, 2017. He said "solidarity is not a one-way street".
German Foreign Minister Sigmar Gabriel said he expects all European Union countries to implement the decision "without further hesitation".
Szijjarto said the decision "endangers the future and security of Europe as a whole".
In their case before the ECJ, Hungary and Slovakia argued that there had been procedural flaws and that the decision was neither a suitable response to the migrant crisis nor necessary to deal with it.
The low level of relocations, the ECJ noted, was partly due to "the lack of cooperation on the part of certain member states".
They were an attempt to ease the pressure on frontline countries such as Greece and Italy.
She said that "leaders such as (Hungarian Prime Minister) Viktor Orban can not demand more money for border protection, while blocking the reception of refugees from Greece and Italy".