BOSTON — The state’s highest court on Friday overturned a conviction in the 2005 murder of a New Bedford man and ordered a new trial, saying the trial judge should not have allowed a secretly recorded prison confession into evidence.

The Massachusetts Supreme Judicial Court ruled unanimously in the case against John Burgos. The justices noted that wiretaps are generally prohibited under state law unless police can show an alleged crime was committed “in connection with organized crime.”

That specific requirement, which the court had also highlighted in a previous ruling, prevents use of electronic surveillance to investigate shootings and killings committed by street gangs, which are among the toughest to solve and prosecute.

Burgos was convicted in 2010 of first degree murder in the killing of Dana Haywood.

The justices said that the state’s case hinged largely on a conversation recorded by Burgos’ cellmate years after the killing.

In the 2009 recording, Burgos admits to murdering Haywood “executionist style” and provides some details about the shooting, including that it was in retaliation for the slaying of a fellow gang member.

But the judges concluded that police didn’t provide sufficient evidence to back up their claim that Hayward’s murder was part of a turf war between two rival street gangs dealing in drugs when they sought and received a court-approved warrant for the wire.

No eyewitnesses identified Burgos as the shooter, they stated. And DNA evidence “at best” placed Burgos at the scene.”A retaliatory killing alone, without a clear link to the goals of a criminal enterprise, does not amount to a connection to organized crime,” the judges wrote.