Updated

A judge has dismissed a murder case and ordered the release of the defendant — an Arizona man convicted in the 2004 death of his 5-year-old son — after finding misconduct by prosecutors.

Maricopa County Superior Court Sally Duncan issued her ruling on Tuesday in the case against Jeffrey Martinson. As part of her decision, she said Martinson cannot be retried.

The judge ordered Martinson to be released on Nov. 26 after he spent nine years in prison. Prosecutors hope to keep him behind bars with an appeal.

"The court is mindful of what is at stake in this case. The allegations against the defendant are very serious," Duncan wrote, adding that the "scope and extent of the misconduct in this case leaves the court with no alternative but to dismiss the case with prejudice."

Martinson, 47, was accused of giving a drug overdose to his son then attempting suicide. Lab tests showed the child had toxic levels of a muscle relaxer in his system. Martinson was involved in a custody dispute with the boy's mother at the time. He has maintained his innocence.

"We're elated about this ruling. We've been fighting for this for years," Martinson's attorney, Michael Terribile, said Wednesday. "We're looking forward to his release."

Maricopa County Attorney Bill Montgomery said he plans to appeal the judge's ruling and hopes to keep Martinson jailed until the matter is resolved.

He wouldn't comment on the specific allegations of prosecutorial misconduct but said his office will conduct an internal review of the case.

"There are certainly aspects of what she set forth that I know are flat out wrong," Montgomery said of the judge's order.

The crux of the judge's ruling involved the charges against Martinson. He faced a felony murder charge in which prosecutors aimed to prove that he unintentionally killed the child during the commission of child abuse, not in a premeditated act.

At trial, however, Judge Duncan noted, prosecutors repeatedly tried to convince the jury the killing was intentional, meaning it was premeditated murder — a specific charge that had not been filed against Martinson.

The shifting theories of the crime confused jurors, Duncan wrote, and violated the trial judge's orders to prosecutors that they stick to arguments over the crime charged.

Jury confusion became clear from the moment deliberations began, Duncan noted.

"I have a juror who is refusing to focus on the charges as presented and instead wants to focus on premeditation," the jury foreman wrote in a note to the trial judge.

Among other allegations of misconduct before, during and after the original trial, Duncan said the prosecution's effort to impose a "win-by-any means" strategy violated the defendant's right to a fair trial.

"It is impossible to know upon what charge the jury convicted the defendant," Duncan wrote.

"The court does not take this action lightly," the judge continued, "but with a somber and well-considered belief that the public's confidence in the integrity of its criminal prosecutions requires no less."

Terribile noted that his client will never get a chance to have his guilt or innocence proven by a jury of his peers.

"That's the harm prosecutors caused. We can't dissipate the allegation," he said. "That's the shame of it."

Martinson was initially convicted in 2011 after years of delays due to changing defense lawyers. A judge then declared a mistrial in his sentencing phase when jurors were unable to reach a decision on a push by prosecutors for the death penalty.