The security window of a cell door was broken by a mentally ill inmate in crisis at the Montgomery County Correctional Facility. (Astrid Riecken/For The Washington Post)

THE NUMBER of inmates in jails and prisons nationally has been dropping for nearly a decade, mainly owing to lower crime rates. That’s the good news. The bad news is that the number of mentally ill people in many detention facilities — local jails, especially — has been soaring, forcing the institutions into the role of treatment centers, for which they are unprepared.

A typical case, in many ways, is the Montgomery County jail. Its average daily inmate count has dropped by a third so far this decade; at the same time, the raw number needing immediate mental-health services has doubled, to more than 2,100 annually. More than a fifth of all inmates now suffer from schizophrenia, bipolar disorder, clinical depression or other serious illnesses.

Mindful of the strain on the jail, and the futility of locking up mentally ill people who commit relatively minor crimes, the county is moving swiftly to establish mental-health courts. It’s an excellent idea; in other localities around the country and in Maryland, similar courts have cut recidivism rates by at least 20 percent and enhanced quality of treatment along with chances of recovery.

The idea, similar to existing drug courts, is to divert qualified defendants charged with nonviolent crimes — trespassing, vandalism, shoplifting, theft, public disorderliness or drunkenness — from jail to supervised treatment regimens. Designated local judges would still oversee individual cases, and prosecutors and defense attorneys would have to assent. Rather than incarceration, defendants would be assigned programs designed to treat their illnesses, with mental-health counselors substituting for jail guards. Criminal charges would typically be suspended as long as defendants stuck to prescribed medication and counseling, among other conditions.

The courts, still awaiting a final decision on county funding, would not be a panacea for a decades-long policy of deinstitutionalization that has left states and localities woefully short of hospital beds and other facilities for the mentally ill. (In Maryland, for instance, more than 80 percent of state psychiatric beds have been eliminated since 1982.) As a pilot program in Montgomery, expectations for the initial impact of the mental-health courts are modest — perhaps 200 defendants would be enrolled and spared incarceration in the first year and half, starting this fall.

Still, in concert with similar-minded programs, including one targeted at easing the way back into the community for soon-to-be-released mentally ill inmates, these courts represent an enlightened and humane step forward in criminal justice. As a means of reducing recidivism, the 300 similar courts already up and running across the country have also proved cost-effective.

Credit in Montgomery goes to the county’s top prosecutor, John McCarthy, and former County Council member Phil Andrews, who led a task force at Mr. McCarthy’s behest that turned discussion of mental-health courts into an actionable plan. The chief judges of the county’s district and circuit courts, Eugene Wolfe and John W. Debelius III, respectively, were also instrumental. With luck, the fledgling courts expected to launch this fall will become permanent.