The European Union approved the controversial scheme two years ago amid Europe's worst migrant crisis since World War II but Hungary and Slovakia went to court to block the plan, backed by other eastern member states.
The European Court of Justice said: "The Court dismisses the actions brought by Slovakia and Hungary against the provisional mechanism for the mandatory relocation of asylum seekers."
In rendering its verdict, the court upheld the right of EU institutions to "respond effectively and swiftly to an emergency situation characterised by a sudden inflow of displaced persons."
It also held that the European Council, the body gathering the member states, "was not required to act unanimously when it adopted the contested decision."
It was referring to the decision in Brussels to relocate 120,000 Syrian and other asylum seekers from overstretched Greece and Italy to most of the other 28 EU member states. It is part of a scheme to relocate a total of 160,000 asylum seekers.
Officials in Brussels have argued the scheme is legally binding on member states, including those who voted against the quotas like Hungary, Slovakia, the Czech Republic and Romania.
Poland initially supported the plan but has come out strongly opposed since a right-wing government came to power weeks later.
The court statement said that Poland intervened in support of Hungary during the proceedings, while the executive European Commission, along with Greece, Italy, Germany, Sweden and several other member states, backed the relocation plan.
Eastern European member states opposed the plan, saying they were not equipped to integrate people from mainly Muslim countries.
Brussels launched the relocation scheme in September 2015, the year more than one million migrants arrived in Europe by sea.
It was introduced as an exception to the so-called Dublin rules under which migrants must apply for asylum in the member state where they first land.
Under international and European law, countries are required to grant asylum to people fleeing war or persecution but not those classed as economic migrants.
Political pressures have eased with a decline in migrant flows.
This is partly a result of a deal the EU signed with Turkey to send back migrants in return for billions of euros in aid and for admitting asylum seekers directly from refugee camps in Turkey.
(This story has not been edited by NDTV staff and is auto-generated from a syndicated feed.)