WASHINGTON--The Supreme Court on Wednesday held that defendants who plead guilty retain the right to challenge their convictions as unconstitutional, frustrating prosecutors who said the rule would inject uncertainty into a criminal justice system built on plea bargains.

The 6-to-3 vote upset the court’s ideological alliances, as Justice Neil Gorsuch joined Chief Justice John Roberts and four liberal colleagues in the majority. The decision allows a North Carolina man who pleaded guilty to bringing loaded weapons onto the...