Victims groups warned dozens of prisoners could be freed after the ruling.
The Strasbourg-based court also told Madrid to pay £25,000 compensation to Ines del Rio, who was jailed in 1989 for her role in 23 assassinations and car bombings carried out by Basque independence movement ETA.
Del Rio had been scheduled for early release in 2008, but courts extended her detention and those of dozens of other ETA members.
Upholding an earlier ruling that had been appealed by Spain, judges at the European Court of Human Rights overwhelmingly decided her rights had been violated by the retroactive changes to sentencing rules.
"I feel indignation, nausea, rage, anger over the decision," said Enrique Gonzalez, a member of Spain's Guardia Civil police force, who was wounded when ETA's Madrid cell, which del Rio belonged to, attacked a bus in 1985.
Groups representing victims of political violence called on the Spanish government not to comply with the sentence and not allow it to set a precedent.
"Terrorists will get out of jail," the president of Spain's Terrorist Victims Association, Angeles Pedraza, said earlier.
Del Rio was found guilty of murder, attempted murder and other crimes in a number of trials.