The Supreme Court ruled for the inmate in a death row case Thursday. (Jon Elswick/AP)

A divided Supreme Court decided Thursday that judges must recuse themselves from cases in which they played a significant previous role in prosecuting the person before them.

The court ruled 5 to 3 that a former Pennsylvania Supreme Court justice, Ronald Castille, should not have considered the appeal of a death row inmate because as a prosecutor 30 years earlier Castille had made the decision to seek the death penalty against that man.

“Where a judge has had an earlier significant, personal involvement as a prosecutor in a critical decision in the defendant’s case, the risk of actual bias in the judicial proceeding rises to an unconstitutional level,” wrote Justice Anthony M. Kennedy, who was joined by the court’s liberal justices.

Kennedy wrote that Castille — Philadelphia’s former district attorney who was elected to the state’s high court and is now retired — should have stepped aside.

“The due process guarantee that ‘no man can be a judge in his own case’ would have little substance if it did not disqualify a former prosecutor from sitting in judgment of a prosecution in which he or she had made a critical decision,” Kennedy wrote.

The decision was one of three issued by the court Thursday. It also said federal judges have a limited ability to recall discharged jurors in some civil cases, and held that Puerto Rico could not prosecute a person for a particular crime if the federal government has already done so.

With the court’s term coming to a close at the end of the month, the justices still have important outstanding rulings on affirmative action in college admissions, on the restrictions states may place on abortion clinics and on President Obama’s plan to shield millions of undocumented immigrants from deportation.

The decision in the judge-recusal case mirrored a 2009 decision in which the court split ideologically over the issue, with Kennedy providing the deciding vote. In Caperton v. Massey, the court said a West Virginia Supreme Court justice should have recused himself because of campaign contributions tied to one of the parties in the case.

In both cases, Chief Justice John G. Roberts Jr. and the court’s most consistent conservatives disagreed.

In the Pennsylvania case, Roberts wrote Thursday, it might have made sense for Castille to recuse, but his involvement did not rise to the level of a constitutional violation.

Roberts wrote that “the majority opinion rests on proverb rather than precedent, ignoring previous rulings that there is “a presumption of honesty and integrity in those serving as adjudicators.”

On the specific procedural claim being considered by the Pennsylvania Supreme Court, Roberts added, the death-row inmate “does not allege that Chief Justice Castille had any previous knowledge of the contested facts at issue . . . or that he had previously made any decision on the questions raised by that petition.”

Justice Samuel A. Alito Jr. joined Roberts’s dissent, and Justice Clarence Thomas filed his own objection.

The majority threw out a 2014 ruling by the state high court that upheld the death penalty for death row inmate Terrance Williams after a lower court had decided he should get a new hearing.

The complicated case began in 1986, when the teenage Williams, a star football player, killed church deacon Amos Norwood and set his body on fire. Then-prosecutor Castille authorized his lawyers to seek the death penalty, and Williams once came within days of being executed.

Prosecutors said the crime was tied to a robbery. But during the appeals process, lawyers for Williams found evidence that the teenager had been sexually abused by Norwood, something Williams himself had not acknowledged. A lower court found Castille’s prosecutors had withheld that evidence, although there was no claim that Castille knew that. As part of his campaign for the elected state supreme court, Castille had made an issue of his success in securing the death penalty.

Kennedy said neither the passage of time nor the fact that the rest of the state high court turned down Williams’s appeal alleviated Castille’s duty to recuse.

“The fact that the interested judge’s vote was not dispositive may mean only that the judge was successful in persuading most members of the court to accept his or her position,” Kennedy wrote. “That outcome does not lessen the unfairness to the affected party.”

The decision means that Williams should receive a new hearing. As a practical matter, there is a moratorium on executions in Pennsylvania. The case is Williams v. Pennsylvania.

In Dietz v. Bouldin, the case involving whether jurors could be recalled, the court issued a narrow and pragmatic ruling. Justice Sonia Sotomayor wrote for the majority that judges have a limited ability to do so but must be careful to ensure that nothing in the interim would prejudice the jurors.

The Supreme Court was considering a case where jurors had just been discharged after awarding damages in an automobile accident lawsuit and the judge discovered a technical mistake. He recalled them to correct it, which Sotomayor said passed muster.

But both she and the dissent — by Thomas, with Kennedy agreeing — said an era of immediate communications required special diligence.

“It is a now ingrained instinct to check our phones whenever possible,” Sotomayor wrote. “Immediately after discharge, a juror could text something about the case to a spouse, research an aspect of the evidence on Google, or read reactions to a verdict on Twitter. Prejudice can come through a whisper or a byte.”

Thomas said that was reason for retaining restrictions on calling back the jury in any circumstance. “In today’s world of cellphones, wireless Internet, and 24/7 news coverage, the rationale that undergirds the bright-line rule supplied by the common law is even more relevant,” he wrote.

In the other case, Commonwealth of Puerto Rico v. Sanchez Valle, Justice Elena Kagan wrote an opinion agreeing with the Puerto Rico Supreme Court that the commonwealth’s prosecutors were barred from pursuing criminal charges against those already convicted under federal law.

The same criminal activity can sometimes be prosecuted under state and federal laws. But Kagan said Puerto Rico’s special status barred that in the case at hand, which involved illegal gun sales.

“The oldest roots of Puerto Rico’s power to prosecute lie in federal soil,” Kagan wrote.

Sotomayor and Justice Stephen G. Breyer dissented.