A bitterly divided Supreme Court on Monday ordered California to reduce its chronically overcrowded prisons by more than 30,000 prisoners, saying judges must get involved when prison conditions are “incompatible with the concept of human dignity.”

Against the backdrop of an exploding national prison population, the court in a 5 to 4 decision upheld a special lower court panel’s findings that California’s prison conditions are so extraordinary as to violate the Constitution’s protection against cruel and unusual punishment.

Because it is unrealistic to believe that the nation’s largest state can “build itself out of this crisis,” Justice Anthony M. Kennedy, a California native, joined the court’s four consistent liberals in agreeing that it is time for the courts to force the state to act.

“The release of prisoners in large numbers — assuming the state finds no other way to comply with the order — is a matter of undoubted, grave concern,” Kennedy wrote. “Yet so too is the continuing injury and harm resulting from these serious constitutional violations.”

Perhaps mindful of the public reaction to one of the largest prison reduction orders in the nation’s history, Kennedy listed various ways that state officials could reduce the prison population without releasing dangerous criminals, including transfers to local jails or prisons out of state.

And he underscored the depth of the prison system’s problems by taking the rare step of including photos in his opinion showing the overcrowded conditions — huge rooms jammed with tight rows of bunk beds and phone-booth-size cells in which some prisoners were held.

But his opinion was met with two scorching dissents.

Justice Antonin Scalia, signaling the extent of his disagreement by reading his dissent from the bench, said his colleagues were affirming “perhaps the most radical injunction issued by a court in our nation’s history,” and he warned of “terrible things sure to happen as a consequence.’’ of this outrageous order.”

Justice Samuel A. Alito Jr., in a dissent joined by Chief Justice John G. Roberts Jr., added: “The majority is gambling with the safety of the people of California. . . . I fear that today’s decision, like prior prisoner release orders, will lead to a grim roster of victims. I hope that I am wrong.”

Perhaps the most low-key reaction on a day of high emotions was from California Gov. Jerry Brown.

The California legislature has approved a Brown-instigated proposal that could eventually transfer 40,000 state inmates to local jails, and he said in a statement that the decision should spur lawmakers to fully fund the plan.

“As we work to carry out the court’s ruling, I will take all steps necessary to protect public safety,” Brown said in a statement.

David Fathi, director of the American Civil Liberties Union National Prison Project, said the national significance of the decision was that it “reaffirms that courts will step in when necessary” to ensure that states maintain prison conditions that meet constitutional standards.

But he noted that judicial orders requiring prison population reductions are exceedingly rare and that California’s prisons are a special case.

The number of prisoners in the state continues to fluctuate, but at one time during the two decades of litigation in the case the prison system held nearly twice as many inmates as the 80,000 for which it was designed.

Two classes of inmates — one alleging inadequate mental-illness care and the other complaining of a lack of medical care — brought suit against the state. A special three-judge panel said that the overcrowding made it impossible to deliver the care and that the population needed to be reduced by about 46,000 inmates to reach a more manageable 110,000.

The prison population has been reduced some since then, and an attorney for the inmates said the number of inmates now needs to come down by about 32,000.

Acknowledging the possibility that “mistaken or premature release of even one prisoner can cause injury,” Kennedy nonetheless went to lengths to document the prison conditions, saying the lower court was right to decide that they violated the Eighth Amendment’s prohibition against cruel and unusual punishment.

“As many as 200 prisoners may live in a gymnasium, monitored by as few as two or three correctional officers,” he wrote. “As many as 54 prisoners may share a single toilet.” Suicides averaged one a week, he said; a report found that one inmate died about every week from ailments that could have been prevented.

In addition to transferring inmates to other facilities, which California is already doing, Kennedy suggested other ways to bring down the population.

Expanding the use of “good time” credits would allow the release of those least likely to re­offend; so would excusing prisoners now incarcerated for technical violations of parole.

Kennedy was joined by Justices Ruth Bader Ginsburg, Stephen G. Breyer, Sonia Sotomayor and Elena Kagan, all of whom had expressed dismay during oral arguments that the state had not moved faster to fix the problems.

Advocates for prison reform picked up Kennedy’s thread, saying that states and the federal government are simply locking up too many people for insignificant crimes and that public money could be better spent on less-expensive alternatives.

Kent Scheidegger of the Sacramento-based Criminal Justice Legal Foundation had different advice for Californians: “Buy a gun. Get a dog. Put in an alarm system.”

Even though the three-judge panel said the reductions should be accomplished within two years — the time would begin running now — Kennedy said the state “may wish” to petition the lower court for five years to accomplish the task.

Scalia called that a “bizarre coda” to the decision. Perhaps, he wrote, it is “nothing more than a ceremonial washing of the hands — making it clear for all to see, that if the terrible things sure to happen as a consequence of this outrageous order do happen, they will be none of this court’s responsibility.” Scalia’s dissent was joined by Justice Clarence Thomas.

Scalia said federal judges had no business trying to run social institutions, and that his concerns “are doubly magnified when they force prison officials to release convicted criminals.”

He noted that the prisoners who brought the suit would not necessarily be the ones slated for release; rather, those leaving prison would be “just 46,000 happy-go-lucky felons” fortunate enough to be selected.

Alito said the lower court did not abide by the requirements of the Prison Litigation Reform Act of 1995, which he said was enacted to prevent massive releases.

“The three-judge court would have us believe that the early release of 46,000 inmates will not imperil — and will actually improve — public safety,” Alito wrote. “Common sense and experience counsel greater caution.”

The case is Brown v. Plata.