The Spanish Constitutional Court blocked the Senate from voting on a bill that included changing the way its judges are picked, raising political tensions as Spain heads into an election year.
The court, which has a conservative majority, accepted the centre-right opposition People's Party's (PP) request to halt the vote on a statutory reform that would reduce the majority needed to pick judges.
It could have resulted in the removal of some conservative judges and a swing to the left in the balance power at the Constitutional Court.
Prime Minister Pedro Sanchez, leader of the Socialist Workers' Party, said the government would comply with the ruling, but would appeal.
"There are no precedents for this. It's the first time this has happened in Europe," Sanchez said.
An intervention blocking lawmakers from voting on legislation raises fears about democratic institutions just 44 years after the dictatorship of Francisco Franco.
Until now, a three-fifths parliamentary majority was needed to pick judges, which has proved impossible without the support of the PP.
The lower house had approved the bill last week.
A third of the judges' mandates have expired and political parties have been unable to agree on replacements for more than four years. The renewal of judicial bodies needs qualified majorities.
PP representatives welcomed the court decision, with one saying: "The rule of law has won."
Pablo Simon, a professor in political science at the Carlos III University in Madrid, said in its bid to speed up the appointment of judges, the government had "ended up generating a far-reaching constitutional and institutional crisis".
The reform was tacked on to other amendments such as the abolition of the crime of sedition and the lowering of penalties for the misuse of public funds when there is no personal gain - measures the opposition said the government was using to court the votes it needs from Catalan separatists to pass laws.
That prompted opposition parties to accuse the government of taking shortcuts to modify a constitutional law and take their case to court.
Simon said the government could still push through its legislative changes by separating the reform of the law on judicial power from the other amendments, a more lengthy judicial process.
The court generally rules on whether specific laws or decrees on socially divisive issues such as abortion, euthanasia or public education are in line with the constitution.
On this occasion, it intervened on legislative procedures, with six conservative-leaning judges of a total 11 voting in favour of blocking the vote.
(This story has been corrected to fix the size of majority required to three-fifths, from two-thirds, in paragraph 7)
(Reporting by Belén Carreño and Emma Pinedo, additional reporting Inti Landauro; Writing by Charlie Devereux; Editing by Aislinn Laing, Arun Koyyur and Nick Macfie)