The court said Wednesday that it had "dismissed in its entirety the actions brought by Slovakia and Hungary".
"The mechanism actually contributes to enabling Greece and Italy to deal with the impact of the 2015 migration crisis and is proportionate", the ECJ said in a statement announcing its ruling.
In response to the migration crisis that hit Europe in the summer of 2015, the Council of the European Union demanded that member countries should help Italy and Greece deal with the migrant arrivals.
The agreement provided for the relocation of up to 160,000 people across member states, but only about 25,000 have been transferred so far.
Hungary and Slovakia asked the court to annul the decision. Hungary and Poland have not accepted any migrants within the mechanism, while the Czech Republic stopped accepting refugees in August 2016.
The two countries defied an European Union plan to resettle 120,000 registered refugees across the 28 member states through a quota system.
So, Kim Kardashian Once Said She Was Taylor Swift's "Biggest" Fan
Taylor and her team reached out to the guys about using the track and Right Said Fred were happy to oblige, a rep for the band told Fader .
Hungary, Poland and the Czech Republic are also facing legal action by the EU executive, the European Commission, for their inaction over the relocation of asylum seekers.
Szydlo says she isn't surprised by the court's decision, but that it "absolutely does not change the position of the Polish government with respect to migration policy". Despite earnest efforts from some European Union leaders, there have not been much of a progress in that area and millions remain stranded in the country they first arrived namely Italy and Greece leading to more political tensions in those two countries.
The Hungarian Helsinki Committee, an advocate for asylum-seekers, urged Hungary to give refugees an opportunity to make their case for asylum.
While Hungary and Slovakia have now failed in their legal challenge to the policy, that doesn't fix the political problem.
The EU was accused of "raping" its own values after its top court ruled countries should be forced to accept refugees under a relocation scheme.
Over the past few years Europe has been faced with millions of refugees and asylum seekers escaping war zones and hardship from across Africa and the Middle East.
Amnesty International welcomed the court's decision. Under the policy, Hungary is required to take in 1,294 refugees and Slovakia 902.
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.
- Tuesday in CE: Google Launches an ARCore Developers Kit for Android Devs
- Kono urges China, Russia to up pressure on N. Korea
- Gas prices spike in London to $131.9/L
- Rouhani: IAEA unlikely to inspect Iran's military facilities
- Kvitova tops Muguruza in US Open fourth round
- Eyes on Jacob Eason in Georgia vs. Appalachian State
- China recounts Pakistan's sacrifices against terrorism ahead of BRICS summit
- Iran rejects US demand for inspection of its military sites
- Bama uses familiar recipe to beat Florida State
- US Open: No. 1 seed Karolina Pliskova rolls into US Open quarters