Poland’s lowering of the retirement age for judges breaches EU law, the European court of justice has said, in a victory for civil society campaigners seeking to preserve democratic values.

The ruling from Europe’s highest court, released on Monday, is a blow to Poland’s nationalist Law and Justice (PiS) government, which has drastically increased control over the judiciary in recent years.

It will also be a disappointment for Hungary’s government, which is also in the EU dock over the rule of law and has made representations to the court in Poland’s favour.

Since taking office in 2015 PiS has assumed direct oversight of state prosecutors and the judicial body that appoints, promotes and disciplines judges. In 2018 the government introduced a law to force the retirement of 40% of supreme court judges.

That measure has now been ruled as inconsistent with the EU fundamental values of democracy and the rule of law.

In a statement the court said that lowering the retirement age of judges to the supreme court was “not justified by a legitimate objective and undermines the principle of the irremovability of judges, that principle being essential to their independence”.

The court also referred to Poland’s obligations under the charter of fundamental rights and EU treaty article 2, which states that the union is founded on values, including democracy and the rule of law.

Lawyers for the Polish government had argued in court that the law was intended to bring judges’ retirement ages in line with those of the general population.

The measure was introduced after President Andrzej Duda rejected an earlier plan that would have instantly dismissed those permitted to serve by the minister of justice, following mass street protests. Duda, a former member of PiS, made his own proposal a few months later to lower the age of retirement from 70 to 65, and the measures came into force last April.

The law meant 40% of Polish supreme court judges had to retire early or apply to Duda to stay in post. The head of the Polish supreme court, Małgorzata Gersdorf, along with 10 other judges, refused to file an application to the president on the grounds that the law violated constitutional provisions on judicial independence.

The ECJ decision will be welcome news to most other EU member states, who are embroiled in a complex and slow-moving exchange with the Polish government over the rule of law. In 2016 Brussels launched an investigation into Poland’s increasing controls on the judiciary – the first time the European commission deployed its rule of law procedure, which could lead to the country stripped of its right to vote.

But the process has become bogged down in the EU council of ministers, with some countries reluctant to criticise a fellow government. Many diplomats hope the ECJ will solve the conflict over the rule of law in Poland through its rulings, obviating any confrontation between Warsaw and other member states.

Poland has now been ordered to pay the costs of the case, while Hungary will bear the bill for its time in court.