The two central EU states, backed by neighbour Poland, had argued that the EU's 2015 scheme to have each member state host a certain number of refugees was unlawful.
The top EU court's adviser said on Wednesday that a case brought by Slovakia and Hungary challenging the obligatory relocation of asylum seekers across the bloc should be dismissed.
The two eastern EU states, backed by neighbour Poland, had argued that the EU's 2015 scheme to have each member state host a certain number of refugees was unlawful.
TRT World's Sarah Morice has more on the story from London.
The programme was designed to help ease pressure on asylum systems in Greece and Italy after mass arrivals across the Mediterranean.
But the court's Advocate General Yves Bot rejected most of the procedural arguments presented by Bratislava and Budapest and said the resettlement scheme was appropriate.
He rejected arguments that the mandatory quotas should have been approved unanimously by EU countries, instead of being agreed by a qualified majority despite the two states' objections.
"The contested decision automatically helps to relieve the considerable pressure on the asylum systems of Italy and Greece following the migration crisis in the summer of 2015 and ... is thus appropriate for attaining the objective which it pursues," he said.
The nationalist-minded, Eurosceptic governments in Warsaw and Budapest have refused to take in a single asylum-seeker under the EU scheme.
Slovakia and the Czech Republic have also stalled, citing security concerns after a raft of terror attacks in the EU in recent years.
Their reluctance to help the two southern frontline states, as well as wealthier EU countries such as Germany, which has taken in hundreds of thousands of migrants, have precipitated bitter disputes in the bloc and weakened its unity.
A final ECJ ruling is expected after the summer break.
The court does not have to, but generally does follow the advisory opinion of the Advocate General.
EU steps up legal cases against Eastern European states
The European Commission said on Wednesday it had stepped up its legal case against the Czech Republic, Hungary and Poland over their reluctance to take in migrants from other EU countries.
Last month the European Commission launched legal proceedings against the countries for failing to take in their share of refugees under the scheme.
"Despite repeated calls for action... these three countries remain in breach of their legal obligations and have shown disregard for their commitments to Greece, Italy and other member states," the EU executive said.
In so-called "reasoned opinions", the Commission has now sent a formal request to these countries to apply EU rules, to which they have one month to respond. Should the Commission not find the answers satisfactory, it can take the countries to court.
Slovakia's Prime Minister Robert Fico said in a statement his government was sticking to its decision to refuse mandatory quotas and called the Advocate General's opinion "non binding".
Hungary dismissed the ruling as politically motivated.
"The main elements of the statement are political, which are practically used to disguise the fact that there are no legal arguments in it," Pal Volner, state secretary of the Justice Ministry, was cited as saying by the state news agency MTI.
The European Commission said that some 24,700 people had been moved from Greece and Italy under the plan that had been due to cover 160,000.
It said it had earmarked 377.5 million euros - or 10,000 euros per person - for 2018 for a twin scheme to legally bring to Europe asylum seekers from places such as Turkey, Libya or Niger, rather than have people risk their lives in perilous Mediterranean crossings operated by smugglers.
A 2016 deal with Ankara drastically cut arrivals from Turkey to Greece, making Italy the main gateway to Europe now, with some 94,400 arrivals so far this year across the sea.
Brussels offered Italy extra money and help, and urged EU states to step up relocations from that country. It also said Rome had to improve registration of those arriving, especially some 25,000 Eritreans, to qualify them for the move.
Other refugee judgments
In other judgments, the Luxembourg-based court said Austria and Slovenia were right in 2016 to send back a Syrian national and two Afghan families back to Croatia to handle their asylum applications, as it was their first country of entry to the EU.
The ECJ also ruled that an Eritrean who had sought asylum in Germany in 2015 was right to expect that country to handle his case because he has already been there for more than three months, rather than be sent back to Italy, through which he had first entered the EU.
A wave of people fleeing the war in Syria and conflict, violence and poverty in other Middle Eastern and African countries in 2015 triggered Europe's biggest migration crisis since World War II.
The migrant crisis has fuelled a split in the EU between frontline states like Italy and Greece and mostly eastern European countries that are opposed to migration and rejected mandatory quotas.