A state appeals court on Thursday reversed the conviction of Msgr. William J. Lynn, the former Catholic Archdiocese of Philadelphia administrator who became the first church official nationwide to be tried and imprisoned for covering up child sex-abuse by priests.

In a 43-page opinion, a three-judge Superior Court panel wrote that prosecutors and a judge misapplied the state's child-endangerment law by claiming Lynn was criminally responsible because he supervised a priest, Edward Avery, when Avery sexually abused an altar boy in the mid-1990s.

The court found the law at the time applied only to people who directly supervised children.

Lynn, 62, has been serving a three- to six-year term in a northeast Pennsylvania prison since his conviction last year. His lawyer, Thomas A. Bergstrom, said he hoped the monsignor would be freed in days.

"It's the right result, and it's the right decision," Bergstrom said. "It's unfortunate that he had to spend 18 months in prison before we got it."

Philadelphia District Attorney Seth Williams, who once hailed the case as a historic prosecution, said his office would most likely appeal the decision.

"I am disappointed and strongly disagree with the court's decision," Williams said.

The ruling elated Lynn's defenders, who said he had been made a scapegoat for the church, and disheartened his critics and advocates for abuse victims. It also seemed likely to reignite an issue that roiled the 1.5 million-member Philadelphia diocese for nearly a decade.

The archdiocese, which paid for Lynn's defense because the charges stemmed from his job, questioned Lynn's sentence when it was handed down last year, asking that it be "objectively reviewed" and adjusted.

"That has happened," it said in a statement Thursday.

In its ruling, Superior Court sided with an argument that Lynn's lawyers had been making for two years, but that Common Pleas Court Judge M. Teresa Sarmina rejected before and after his trial.

The higher court called Sarmina's interpretation "fundamentally flawed." It also found that Lynn could not be convicted as an accomplice to Avery because there was no proof that Lynn knew Avery had been planning or preparing to assault children.

But the appellate judges took pains not to fully vindicate the monsignor or the church.

"We cannot dispute that the commonwealth presented more than adequate evidence to sufficiently demonstrate that [Lynn] prioritized the archdiocese's reputation over the safety of potential victims of sexually abusive priests and, by inference, that the same prioritization dominated [Lynn's] handling of Avery," the ruling said.

As the secretary for clergy under Cardinal Anthony J. Bevilacqua until 2004, Lynn had responsibilities including proposing priests' assignments and investigating complaints against them.

In 2005, a grand jury had excoriated Bevilacqua, Lynn, and the church hierarchy for their handling of abuse claims, but said gaps in the law prevented them from recommending charges.

Lynn's February 2011 arrest, along with the arrests of Avery, two other priests, and a Catholic school teacher, grew from a second grand jury report and sparked a second wave of promised change by the Philadelphia church. Dozens of priests have since been suspended and reinvestigated by the church over past claims of abuse.

Prosecutors in that case portrayed Lynn as a powerful gatekeeper who quietly shuffled abusive priests between parishes, misinformed parishioners, and worked harder to protect the church's reputation than he did to protect children.

In one of the rulings that Lynn's lawyers said tainted the case, Sarmina allowed prosecutors to show the jury decades worth of child-abuse complaints involving Philadelphia-area priests - files that had been locked away in a safe in church offices.

The assistant district attorneys said it was necessary to establish that Lynn's decisions followed a practice or pattern among church leaders.

They said he allowed Avery in the late 1990s to live and celebrate Mass at St. Jerome's, a Northeast Philadelphia parish, despite knowing that Avery had abused a teenager in the 1970s. Avery, who has since been defrocked, pleaded guilty before trial to charges that he sexually assaulted a 10-year-old altar boy at St. Jerome's.

Lynn took the stand for nearly three days in his own defense. He asserted that he lacked the authority to remove abusive priests - only the cardinal had that power, he said - but said he did all he could and much more than his predecessors to keep them away from children.

"I thought I was helping people," Lynn testified. "I thought I was helping priests, and in those circumstances, I thought I was helping victims, as much as I could."

Jurors deliberated for more than 12 days before convicting him of endangerment. (The jury deadlocked on charges against his codefendant, the Rev. James Brennan, who had been accused of trying to rape a 14-year-old boy in 1996.)

Sarmina imprisoned Lynn immediately after the verdict. He has been serving his term at the state correctional institution in Waymart.

Before, during, and since the trial, prosecutors and defense lawyers have clashed over whether Lynn was properly charged. The child-endangerment law that was in effect when Lynn was secretary for clergy assigned responsibility to direct caretakers, such as parents or guardians.

In 2007, after lobbying efforts from advocates for abuse victims, the law was expanded to include employers and supervisors whose subordinates abused minors in their care.

Prosecutors contended that the changes amounted only to a clarification and amplification of the previous law, and thus Lynn could be charged under that statute.

Lynn's defenders argued that the legislation created a new law and a new class of offenders - and could not be retroactively applied to him.

Under the new law, there was little doubt that Lynn potentially could have been held legally responsible for the actions of other priests, said Alan J. Tauber, another member of Lynn's defense team.

Their appeal raised 10 points that they said brought the verdict into question, including Sarmina's decision to allow jurors to hear decades of old abuse complaints. Superior Court, however, said the first two were enough for a reversal and addressed them.

Prosecutors now could ask the higher court to rehear the case, or petition the state Supreme Court to intervene. Tauber said he expected the District Attorney's Office to appeal the decision "to the bitter end."

Bergstrom said Thursday that he had not yet spoken to Lynn, but that the monsignor's relatives were overwhelmed by the news.

"They are very happy," he said. "This is just a wonderful thing for them."

David Clohessy, executive director of the Survivors Network of Those Abused by Priests (SNAP), said he was disappointed by the decision. He said church officials have too often used expensive lawyers to escape responsibility. "I think many, many survivors and betrayed Catholics will feel very sad about this decision," he said.

The archdiocese said that it has changed since Lynn's time as an official and taken steps "to ensure that all young people in our care find a safe and nurturing environment. . . . We recognize that today's news is especially difficult for survivors and their families. We profoundly regret their pain."