By Michele Sinner and Alastair Macdonald

LUXEMBOURG/BRUSSELS (Reuters) - The EU's highest court ruled on Wednesday that member states must take in a share of refugees who reach Europe, dismissing complaints by Slovakia and Hungary and reigniting an east-west row that has shaken the Union's cohesion.

The Mediterranean migrant crisis which prompted mandatory quotas in 2015 for relocating asylum seekers from Greece and Italy has receded, easing immediate pressure to force compliance on nationalist leaders who are making electoral capital from lambasting the EU while benefiting from Brussels' subsidies.

But defiant rejection from some ex-communist states that the EU accuses of slipping on democratic standards - Hungary called the judgment a political "rape" of EU law - indicated no let-up in tensions that are testing the Union's internal cooperation.

In Germany, where Chancellor Angela Merkel is fighting for re-election against opponents who criticize her for taking in over a million migrants at the height of the drama, the interior minister threatened legal action against countries which fail to take in their allotted share of Syrian and other refugees.

But Hungary, where outspoken Prime Minister Viktor Orban has built border fences and made keeping out migrants a key plank of his re-election campaign for next year, branded the European Court of Justice ruling "appalling and irresponsible".

"This decision jeopardizes the security and future of all of Europe," Foreign Minister Peter Szijjarto said.

"Politics has raped European law and values."

Italy, now the main destination for migrants taking to the sea following measures to block the route from Turkey to Greece and from Greece northward, has been prominent in calling for the wealthier Western states to cut EU subsidies to poor neighbors which do not show "solidarity" in taking in migrants.

European Commission President Jean-Claude Juncker has echoed that threat, warning Orban "solidarity is not a one-way street". But the EU executive is also anxious to calm tensions ahead of negotiations on a new seven-year EU budget that will be all the harder because of a hole left by Britain's imminent departure.

Story continues

The Commission said on Wednesday it may seek fines at the ECJ within weeks for Hungary, Poland and the Czech Republic if they do not start or resume taking people from the south. Poland and Hungary, which argue Muslim immigration is a threat to their national security and stability, have in two years taken in none of their respective quotas of about 6,200 and 1,300 people.

SEEKING "UNITY"

However, the executive, which is separately considering trying to suspend eastern heavyweight Poland for undermining the independence of its judiciary, stressed it wanted solutions that would unite the 28-nation bloc. Slovakia's prime minister said he still rejected quotas but was willing to help in other ways.

And as the Union digests a Brexit vote last year that was partly driven by concern over migration, many states that backed the quota system in 2015 and are increasingly exasperated by Polish and Hungarian hostility to Brussels are also reluctant to force through new policies that may bolster eurosceptics across Europe who say the EU is trampling on national sovereignty.

"If we push it through above their heads, they will use it in their anti-EU propaganda at home," one EU diplomat said, while noting that, with the migration crisis dropping out of the headlines, all sides might find a way to resolve the issue.

The Commission is working on reforms to the so-called Dublin rules under which people arriving in the EU must stay to claim asylum in the first country they reach. A mechanism to help the likes of Greece and Italy in emergencies has proved elusive.

However, governments may find it easier to negotiate now that the migration is not quite so prominent in local politics.

The Luxembourg-based ECJ dismissed "in their entirety" the arguments advanced by Hungary and Slovakia with backing from Poland and endorsed the Commission's solution which eventually ordered states to take in nearly 100,000 Syrians and others.

"The mechanism actually contributes to enabling Greece and Italy to deal with the impact of the 2015 migration crisis and is proportionate," the court said in statement.

Difficulties in identifying candidates for relocation - and a reluctance of refugees to settle other than in the wealthier west of Europe - saw fewer than 30,000 people relocated in two years, but the Commission noted an acceleration in the program this year and said it would continue to move people northward.

Among the gravest problems the migration crisis caused was member states reimposing controls on internal EU frontiers. That had jeopardized one of the Union's most cherished achievements and for a time appeared to threaten to disrupt internal traffic.

EU Migration Commissioner Dimitris Avramopoulos said special exemptions that have allowed the likes of Germany, Austria, Denmark and Sweden to control their borders with EU neighbors should now end when they expire in two months' time.

(Additional reporting by Gabriela Baczynska; Writing by Alastair Macdonald; Editing by Andrew Heavens)