The Court of Justice condemned the law which reduced judges' retirement age to 62 from 70, a flagship reform of Prime Minister Viktor Orbán's government that Brussels sees as a sign of creeping authoritarianism.
Commenting on the decision, Orbán said the government would need to rewrite the rules anyway as Hungary's Constitutional Court had already annulled the law before the court's decision.
"It was a long time ago that I saw a dead dog being knocked on the head," he told a news conference, adding that the court ruled on a law that did not exist.
"There is no new situation, the government will submit proposals to parliament on how to regulate this issue," he said, without elaborating.
The Court of Justice said the law forcing early retirement was too abrupt, failed to provide any transition for those obliged to quit and was not a sound way to lower the age of public sector workers.
The European Commission brought the challenge to the legislation that has forced hundreds of judges into sudden retirement.
The case was the latest EU attempt to curtail a string of contentious laws pushed through by Orbán's conservative government using a two-thirds parliamentary mandate gained in April 2010.
Lydia Gall, Balkans and Eastern Europe researcher at Human Rights Watch, said the new Hungarian rules should also address the problem of those who were already pushed into retirement.
"The Hungarian government should find judicial positions for any judge forced to resign who wishes to return to work and amend legislation to ensure that no further judges are adversely affected," she said in an e-mail.
EU officials say Orbán's Fidesz party is trying to take hold of independent institutions ranging from the central bank to the judiciary. Budapest counters that its policies modernise Hungary and create jobs by getting more young people into the workforce.
Reding: Hungary must comply with EU ruling
Hungary must now comply with the EU court ruling. Failure to do so would allow the Commission to return to the Luxembourg-based court to seek sanctions and fines.
"The court's judgment is crystal clear," EU Justice Commissioner Viviane Reding said in a statement. "Hungary must now take all the necessary measures to comply with the judgment as soon as possible."
Orbán, who has been dubbed "Viktator" by some opponents in Hungary, denies eroding democratic checks and balances.
But he has been forced to cede ground on his reforms, most notably on changes to the central bank law that caused a row with the European Union and International Monetary Fund earlier this year over the independence of the bank.
Amendments have resolved the standoff, although only after it had held up the talks on IMF/EU financing for months.