The binding judgment came as EU politicians criticised Hungary’s state of emergency law, under which police have questioned more than 100 people over alleged coronavirus “fake news”.
Hungarian Prime Minister Viktor Orban refused to attend Thursday’s European Parliament debate on Hungary.
Nearly 300 people are held in transit zones on the Hungary-Serbia border.
The BBC’s Nick Thorpe in Budapest says the EU Court of Justice ruling paves the way for those asylum seekers – nearly half of them children – to be released, as the ECJ ruling means Hungary must devise new asylum rules.
Two families – from Afghanistan and Iran – sued the nationalist Hungarian government at the ECJ, and they will have to be released, our correspondent says.
Of those detained in the two transit zones, 120 have spent more than a year there.
Hungarian troops patrol a barbed-wire fence on the southern border, erected to keep migrants out.
Erno Simon of the UN Refugee Agency (UNHCR) said the long-term detention of children and other vulnerable people in those transit zones was “completely unacceptable and inhuman”.
The Orban government refuses to accept asylum seekers from non-EU countries, resisting pressure from EU partners such as Italy and Greece for burden-sharing. The crisis triggered by a surge of migration in 2015 has still not been resolved, as many migrant camps are overcrowded and squalid.
Many of the migrants in 2015 were Syrians, Afghans or Iraqis fleeing conflict. In recent years many migrants from sub-Saharan Africa have also risked their lives to reach the EU.
The Iranians in the court case are Abouzar Soltani and his 11-year-old son Armin. Abouzar said he was “very happy” with the ECJ ruling.
“For the past three years I have not been able to love Hungary with all my heart, because I was afraid of the law. But for some time now, with all my heart, I consider Hungary as my own country.”
Concern about Orban powers
MEPs voiced concern on Thursday over Mr Orban’s emergency law, passed on 30 March, granting him power to rule by decree indefinitely during the coronavirus crisis.
The law envisages prison terms of up to five years for “spreading false or misleading information before a broad public” about the virus or the authorities’ response to the pandemic.
EU Commission Vice-President Vera Jourova tweeted that “in Hungary, the emergency powers granted appear more extensive than in other member states” and “the Commission will be very vigilant on how emergency measures which affect democratic values are phased out in the member states”.
What were the ECJ’s grounds for the ruling?
Keeping the asylum seekers in the transit zones is “detention”, the ECJ says, “amounting to a deprivation of liberty” because they cannot legally enter Hungary or return to Serbia.
Concerning the two families who sued, the ECJ says Hungary rejected their asylum claims and Serbia refused to readmit them.
But under EU asylum law, the ECJ argues, 18 months is the maximum that anyone can be detained who is subject to a “return” decision by the authorities.
People seeking international protection in the EU can be kept at a country’s border, but after four weeks maximum a member state must rehouse them on its territory.
The ECJ also says that if such an applicant has no means of subsistence, he or she has a right to financial support to get accommodation.
What now for these asylum seekers?
It is now up to a court in Szeged, Hungary, to issue a new ruling on the two detained families, which must conform to the ECJ verdict.
Lawyer Barbara Poharnok, representing them, said Hungary must now release all those detained illegally in the transit zones.
“If this is not done, we will make sure that courts compel them to do so,” said the lawyer from the Hungarian Helsinki Committee.