The European Commission, Greece, Italy, Germany, Sweden and several other EU countries backed the plan.
It's not clear what consequences there will be, if any, for member states that refuse to take in migrants.
The system was adopted in 2015 at the height of asylum seekers' migration to Europe to escape wars and terrorism in the Middle East and Africa.
"Today's ruling shows that no country can hide from their responsibilities to refugees", Iverna McGowan, Director of Amnesty's European Institutions Office, said in a statement.
Since compulsory quotas were introduced in September 2015, Hungary has not accepted a single asylum seeker, while almost 28,000 people have been relocated under the scheme, far below the 160,000 target.
The quotas' legality was confirmed on Wednesday morning, when judges at the European Court of Justice threw out a joint legal challenge against them by Hungary and Slovakia.
Hungary, Poland and the Czech Republic are already facing...
Star Wars Episode IX directors: who should replace Colin Trevorrow?
Lucasfilm are dead serious when it comes to the Star Wars movies and wants to make sure everything is ideal . He wasn't fired, but Lucasfilm brought on Tony Gilroy to direct the reshoots on " Rogue One ".
Lawmakers React to DACA Decision
Vincent Cucchiara, communications director for Penn State's Bull Moose Party, provided an official statement . Failure to enforce the laws in the past has put our nation at risk of crime, violence and even terrorism.
Florida State drops in the Coaches Poll
West Virginia slipped out of the rankings after losing a heartbreaker to Virginia Tech to open the season. Not even a 59-point outburst could keep Oklahoma State in the top 10.
However, Hungarian Foreign Minister Peter Szijjarto slammed the verdict as "irresponsible", saying it "threatens the security of all of Europe".
The decision upholds the union's right to force member states to take in refugees.
The EU was accused of "raping" its own values yesterday after its top court ruled that countries should be forced to accept refugees under a relocation scheme. Slovakia and Hungary insisted in their lawsuits that the quota mechanism should have been passed unanimously.
Slovakia and Hungary argued the European Union broke its own rules and exceeded its powers when it approved the quota system with a "qualified majority", or about two-thirds vote.
"I was convinced that such a decision would be made [by the court], but this absolutely does not change the stance of the Polish government with respect to migration policy", Szydlo told reporters at a business conference.
Poland initially supported the mandatory quotas but now, with its new right-wing government, is against it.
But Slovakian Prime Minister Robert Fico said he accepted the court's decision. So far only 25,000 refugees have been moved. That measure, including payments of up to €6 billion to help Turkey manage its refugee population and a pledge to reinvigorate talks on the possibility of Turkey joining the bloc, slowed the flow across the Aegean route to a trickle.