Thousands of prisoners might have been in jail longer than they should have been and could be owed millions in compensation.
The country's highest court has found the Corrections Department made mistakes in calculating the release dates for two inmates, and the ruling could now have a ripple effect.
Corrections has so far identified 21 prisoners who will be affected by today's ruling.
The department's deputy national commissioner, Rachel Leota, said some of the affected inmates could be released as early as tomorrow.
She said the department had been prepared for the possibility that the court might rule in the prisoners' favour.
Sentence lengths 'calculated inappropriately' - Supreme Court
Michael Marino and Edward Booth spent lengthy periods in jail on remand but the department failed to accurately factor that in.
Marino was sentenced last year to 22 months' jail and, by his own calculation, should have been released in January this year, based on his time on remand.
When that did not happen, he filed a writ of habeas corpus (a legal proceeding to review his detention) but both the High Court and the Court of Appeal found he was being held legally.
In its decision, released today, the Supreme Court found the calculation used by Corrections and upheld by the lower courts would have given Mr Marino no credit for his time in custody on remand between February and June last year.
In Booth's case, Hon Justice William Young said Corrections' calculation would have led to him being in prison for 12 years and seven months, rather than the total of 11 years and nine months actually imposed at sentencing.
In that case, he will now only serve the latter term.
The Supreme Court found pre-sentence detention related to any period an offender spent in custody from the time they were charged right up to when they were sentenced.
It "unanimously" upheld that the Parole Act 2002 had been misinterpreted, and, as a consequence, there had been instances where parole and release dates had been "calculated inappropriately".
The lawyer who took the case, Douglas Ewen, said there were probably thousands of other such cases and the Crown could be up for millions of dollars in compensation costs.
During the hearing, Corrections' lawyer said there were 30 to 40 inmates a month who would be affected.
The current way of calculating sentences goes back to 2003.
The department had made its calculations on a charge-by-charge basis, and also said different calculations were needed when sentences were cumulative rather than concurrent.
The Supreme Court found that was inconsistent.
"[Corrections] did not give any policy reason for the differentiation it made between concurrent and cumulative sentences.
"We have also already noted that the calculation of pre-sentence detention should be as simple and certain as possible. The interpretation urged on us by [Corrections] would not be in accordance with this policy."
It found Corrections' interpretation of the legislation was also contrary to the Bill of Rights, and Justice Young said it left a lot to chance.
He set out an approach to the sentence end-date calculations, but said, "I do not regard my approach as eliminating all anomalies from the operation of the pre-sentence detention regime which I consider warrants legislative reconsideration."
The court has reserved the question of costs and Marino's legal team has until next week to file an application for those.
Compensation 'only right'
Mr Ewen, who represented Marino, said his client felt vindicated by the court's decision.
He said it should help the department avoid cases like this, where people have sometimes been held not just for weeks and months longer than they should have been, but in one case, years.
Some redress would be sought for Marino, as the state could not unlawfully hold a man for four and a half months and not pay compensation, he said.
"I'm not willing to put a figure on it, but now the Crown's been told they got it wrong, it is only right that they compensate and compensate appropriately."
There would be merit in having Parliament consider the matter, he said - and added that, since sentence length calculations were made by prison officers, not judges, simplicity was needed.