"Due to the ongoing instability and conflicts in the immediate neighborhood of Italy and Greece, and the repercussions in migratory flows on other member states, it is very likely that a significant and increased pressure will continue to be put on their migration and asylum systems, with a significant proportion of the migrants who may be in need of worldwide protection", the Court of Justice found Wednesday.
The mechanism helps frontier countries like Greece and Italy to deal with the impact of the 2015 migration crisis, and the European Union court said the action was proportionate.
German officials were also quick to announce their support for the ruling, with Sigmar Gabriel, the foreign minister, encouraging member states to act swiftly following the decision. The fences have mostly stopped migrants from passing through Hungary on their way to Western Europe but Hungary has also greatly reduced the chances for asylum-seekers to submit applications in the country.
Slovakian Prime Minister Robert Fico said, "we fully respect the verdict of the European Court of Justice", but added that his stance on the quotas "has not changed at all".
The European Commission, the EU executive, welcomed the ECJ ruling.
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.
The Court decreed that the relocation mechanism was not a measure that was inappropriate to achieving its objective, namely helping Greece and Italy to cope with the impact of the 2015 migration crisis.
"ECJ confirms relocation scheme valid".
Hungary and Slovakia asked the court to annul the decision.
"We will continue to work on having solidarity expressed in different ways other than forcing migrants [on Slovakia] from other countries that don't want to be here anyway".
The Hungarian Helsinki Committee, an advocate for asylum-seekers, urged Hungary to give refugees an opportunity to make their case for asylum.
Foreign minister Peter Szijjarto said: 'Politics has raped European law and European values.
He said the slow pace of relocation "draws attention to significant gaps in the EU's response to the biggest refugee crisis on the continent since World War II".
The ECJ could impose large fines or subsidy cuts on the countries as a means of encouraging them to comply, though there is no mechanism to actually for them to take migrants.
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.
The court said Wednesday that it had "dismissed in its entirety the actions brought by Slovakia and Hungary".
A further 7,000 asylum-seekers who arrive before September 26 can be redistributed to other EU countries from Italy and another 4,700 from Greece, according to the European Commission.
INSIDE STORY: Is the European Union closer to solving the migration crisis?