No refugees have been accepted by Hungary or Poland, which saw the conservative Law and Justice party take power after October 2015 elections.
The court said Wednesday that it had "dismissed in its entirety the actions brought by Slovakia and Hungary". "That mechanism actually contributes to enabling Greece and Italy to deal with the impact of the 2015 migration crisis and is proportionate", the judges said in their ruling.
In September 2015, the heads of the EU interior ministries adopted the European Commission's proposal on the distribution of 160,000 undocumented migrants in Italy and Greece, across the European Union under a quota plan.
He added that Budapest will take all legal action possible to prevent relocating people into Hungary "against the will of the Hungarian people".
Slovakia and Hungary which, like the Czech Republic and Romania, voted against the adoption of the contested decision in the Council, and asked the ECJ to annul the decision.More news: Narendra Modi meets Myanmar's state counsellor Aung San Suu Kyi
Slovakian Prime Minister Robert Fico said he respected the court decision, but that his government still does not like the relocation plan, which he and others in Eastern Europe see as quotas imposed on them by unelected European Union bureaucrats in Brussels.
If Hungary, Poland and the Czech Republic do not cooperate with the relocation scheme, they could be referred to the ECJ and face heavy fines.
Hungarian Prime Minister Viktor Orban's government has made migrants a key political issue since early 2015 and ahead of the parliamentary election expected in April 2018. Slovakia and Hungary challenged the decision.
The EU's relocation plan - which expires on September 26 - has not been fully executed, in no small part because of some member states' reluctance to accept migrants.
The fences have mostly stopped refugees from passing through Hungary on their way to western Europe but Hungary has also greatly reduced the chances for asylum seekers to submit applications for protection in the country.More news: Prince William, Kate expecting third child, Kensington Palace says
Germany's foreign minister is welcoming the ruling by a European Union court that Hungary and Slovakia must participate in an EU plan to relocate refugees. Of those, 19,200 were transferred from Greece and 8,212 from Italy.
European Home Affairs Commissioner Dimitris Avramopoulos had said he regretted how certain member states "continue to show no solidarity and to ignore our repeated calls to participate in this common effort".
He said migrant arrivals in Greece from Turkey had dropped 97 percent since the deal in March past year, and that more than 8,800 Syrians in Turkey had now found homes in Europe.
Hungary accused the Luxembourg court of making an "outrageous and irresponsible" politically driven decision.
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.More news: New Uber boss targets IPO within 18-36 months
"The Hungarian and Slovakian governments" appeal to ECJ was a baseless attempt to make "refugee free zones' of their countries".