Chief Justice John G. Roberts Jr. joined with the Supreme Court’s liberals Wednesday in ruling that states may forbid judicial candidates from personally soliciting campaign contributions.

Rejecting the charge that the restrictions violate the First Amendment’s guarantee of free speech, the 5-to-4 decision upheld a Florida law that is similar to others in 29 states that elect judges but restrict their role in campaign fundraising.

“Judges are not politicians, even when they come to the bench by way of the ballot,” Roberts wrote. “And a state’s decision to elect its judiciary does not compel it to treat judicial candidates like campaigners for political office.”

The result was exceptional in two ways. It separated Roberts from his usual colleagues on the right, and it was a rare win for advocates of campaign finance restrictions after a nearly unbroken string of rulings from the high court that the First Amendment trumps such restrictions.

The difference, Roberts said, is that the public expects judges to be fair and disinterested players, while other elected officials are free to follow the wishes of those who support them.

“In deciding cases, a judge is not to follow the preferences of his supporters, or provide any special consideration to his campaign donors,” Roberts wrote, adding that “therefore, our precedents applying the First Amendment to political elections have little bearing on the issues here.”

And he chose a homey analogy in explaining why there is a difference between candidates asking for money and someone asking for money on their behalf.

“The identity of the solicitor matters, as anyone who has encountered a Girl Scout selling cookies outside a grocery store can attest,” Roberts wrote. “When the judicial candidate himself asks for money, the stakes are higher for all involved.”

Roberts was joined by Justices Ruth Bader Ginsburg, Stephen G. Breyer, Sonia Sotomayor and Elena Kagan.

His fellow conservatives responded with a barrage of somewhat disbelieving dissents.

The majority opinion “flattens one settled First Amendment principle after another,” Justice Antonin Scalia wrote, adding that the axiom that the state may not ban speech on the basis of its content “does not grow weaker merely because the censored speech is a judicial candidate’s request for a campaign contribution.”

“The First Amendment is not abridged for the benefit of the Brotherhood of the Robe,” he wrote.

Justice Anthony M. Kennedy added: “Whether an election is the best way to choose a judge is itself the subject of fair debate. But once the people of a State choose to have elections, the First Amendment protects the candidate’s right to speak and the public’s ensuing right to open and robust debate.”

Kennedy’s dissent sounds almost exactly like what Roberts said at oral arguments in the case.

And the two seem to have developed different concerns since a 2009 case, Caperton v. Massey, in which the court ruled that a West Virginia Supreme Court of Appeals justice should have recused himself in a dispute involving someone who had contributed more than $3 million to a campaign to elect the judge. Kennedy wrote the majority opinion, Roberts the dissent.

In the case decided Wednesday, Florida had maintained that its restrictions on judicial candidates asking directly for contributions were born of corruption scandals that led to the resignations of several state Supreme Court justices. Challengers said the state’s regulations were too porous to accomplish the goal of strengthening public confidence in its elected judges.

While candidates in the state may not directly solicit a contribution, they may organize a committee to ask for money, direct the committee toward potential contributors, see who gave money and even send thank-you notes.

The challenge was brought by Tampa lawyer Lanell Williams-Yulee. When she announced her candidacy for a county judge’s seat in 2009, she sent out a mass mailing over her signature that said an “early contribution of $25, $50, $100, $250, or $500 . . . will help raise the initial funds needed to launch the campaign and get our message out to the public.”

She got no responses and eventually lost the race in a landslide to the incumbent.

But the Florida Bar took exception to her actions, saying the letter violated Canon 7C(1) of the Florida Code of Judicial Conduct, which bans direct solicitations. She was reprimanded and fined.

Federal judges, such as the justices, are appointed to their positions. In 39 states, at least some members of the judiciary are elected, and the amount of money raised for those races has exploded. For state supreme courts alone, the total increased from about $90 million over the 1990s to $207 million during the next decade.

Former justice Sandra Day O’Connor has led a campaign to persuade states to give up electing their judiciary, with little success.

Roberts said in his opinion that the Supreme Court was taking no position on that issue. But the decision did win rare praise from groups that worry about judicial elections and campaign contributions in general.

“Today’s decision helps judges by saving them from the compromising job of raising cash from people whose cases they will decide,” said Bert Brandenburg, executive director of the group Justice at Stake. “It helps our court system by shoring up its ability to be fair and impartial. And it helps the public by reassuring them that they will not find themselves in court before a judge who has received a check directly from the opposing party in their case.”

The case is Williams-Yulee v. Florida Bar.