The EU's top court has rejected a challenge by Hungary and Slovakia to a migrant relocation deal drawn up at the height of the crisis in 2015.
The European Court of Justice overruled their objections to the compulsory fixed-quota scheme.
Hungary has not accepted a single asylum seeker under the scheme since it was introduced two years ago.
It was an attempt to ease the pressure on frontline countries such as Greece and Italy.
But the ruling has sparked fury, with Hungary's foreign minister vowing: "The real fight starts now."
Since 2014, about 1.7 million migrants have tried to make new homes in the EU in the worst migrant crisis since World War Two.
Those fleeing war and persecution, many from the Middle East, are entitled to asylum under European and international law.
The numbers peaked in 2015, and in September that year, European leaders agreed to spread a total of 160,000 migrants "in clear need of international protection" among member states over two years.
Hungary, Slovakia, the Czech Republic and Romania voted against the quotas.
The issue was decided by a majority vote - usually followed on issues that do not affect national sovereignty.
Hungary was asked to take 1,294 asylum seekers, Slovakia 802.
To date, Hungary has refused to take a single asylum seeker, along with Poland, while Slovakia has accepted only about a dozen. The Czech Republic has refused to take any for the past year.
Only 28,000 people have actually been relocated under the scheme. But migrant arrivals have fallen overall due to initiatives such as the EU-Turkey deal and EU measures to curb migration from Libya.
In asking the court to annul the deal, Hungary and Slovakia argued at the Court of Justice that there were procedural mistakes, and that quotas were not a suitable response to the crisis.
Officials say the problem is not of their making, that the policy exposes them to a risk of Islamist terrorism and that it represents a threat to their homogenous societies.
Their case was supported by Poland, where a right-wing government has come to power since the 2015 deal.
But it was rejected by the ECJ which argued that the agreement "actually contributes to enabling Greece and Italy to deal with the impact of the 2015 migration crisis and is proportionate".
It rejected the complainants' argument that the scheme should have been adopted unanimously.
Hungary's Foreign Minister Peter Szijjarto was furious, calling it "appalling and irresponsible". He vowed to use all legal means against the judgement, which he said was "the result of a political decision not the result of a legal or expert decision".
"Politics has raped European law and European values. This decision practically and openly legitimates the power of the EU above the member states," he said.
"The real fight starts now."
In a milder statement, Slovakian Prime Minister Robert Fico said his country's position on quotas also "does not change".
EU Migration Commissioner Dimitris Avramopoulos tweeted: "Time to work in unity and implement solidarity in full."
In other comments, he lamented that some member states "continue to show no solidarity".
German Foreign Minister Sigmar Gabriel urged "all European partners to... implement the agreements without delay."
Mr Avramopoulos warned after the meeting that if those nations resisting the scheme did not change their ways, "we should consider to take the last step in the infringement procedure, taking Poland, Hungary and the Czech Republic to the European Court of Justice".
Hungary, Poland and the Czech Republic are already facing legal action by the EU executive, the European Commission, for their inaction over the relocation of asylum seekers.
The three states could be referred to the European Court of Justice (ECJ) and eventually face heavy fines.
The European Union regards the policy of relocating migrants who've crossed the Mediterranean as an important expression of political solidarity between member states.
Under the policy it set quotas for each country to accept fixed numbers of migrants to ease the direct burden on Italy and Greece - the usual points of arrival.
While Hungary and Slovakia have now failed in their legal challenge to the policy, that doesn't fix the political problem. The European Commission may still have to threaten financial penalties against countries that don't co-operate if the ruling from Luxembourg doesn't change their minds.