The court said that it had "dismissed in its entirety the actions brought by" the two member states. "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.
"The Council of the European Union adopted a decision in order to help Italy and Greece deal with the massive inflow of migrants", the court said in a statement Wednesday.
The European Union decision to relocate a total of 120,000 of the roughly one million people who had arrived in Europe's south, mainly from Syria, by September 2017, was backed by most of the bloc's countries, including Poland's previous government.
Hungary and Slovakia had asked the court to annul the scheme, claiming that "adoption of the decision was vitiated by errors of a procedural nature or arising from the choice of an inappropriate legal basis". Hungary and Poland have not accepted any migrants within the mechanism, while the Czech Republic stopped accepting refugees in August 2016. The decision is also likely to be welcomed by other countries such as Greece and Sweden, where the sheer volume of new arrivals threatens to overwhelm the system.
"The door remains, it is still open, and we should convince all member states to fulfill their commitments", EU Migration Commissioner Dimitris Avramopoulos said.
Back in June, dismayed over the failure of some member states to accept asylum-seekers, the European Commission chose to send letters of formal notice to Poland, Hungary, and the Czech Republic, which are the first step toward opening cases against the countries for not living up to their legal obligations.
Hungary's government built security fences on its borders with Serbia and Croatia two years ago to stop refugees and migrants entering the country along the so-called "Balkan route" from Turkey to Germany.
"In contravention of the founding treaties, the ruling asserts the power of the European Commission over EU member states".
However, Gabriel's Hungarian counterpart, Peter Szijjarto, took the opposite view, declaring the ECJ's ruling unacceptable.
"Politically, it enforces the concept of shared responsibility, which means backing for those countries that support distributions of asylum seekers", she said. Of those, 19,200 were transferred from Greece and 8,212 from Italy. So far only 25,000 refugees have been moved. Under the policy, Hungary is required to take in 1,294 refugees and Slovakia 902.