The D.C. government agreed in court yesterday to pay $12 million to settle a federal lawsuit filed on behalf of inmates who alleged that they were jailed longer than they were supposed to be.

Even when charges were dropped or cases were dismissed in D.C. Superior Court, inmates typically were returned to the jail to be cleared for release by the D.C. Department of Corrections -- a process that included a strip-search and that often kept them locked up for hours and sometimes days.

The Corrections Department, as part of the agreement approved yesterday, had recently stopped bringing such inmates back to the jail from court, transporting them instead to a satellite facility at the old D.C. General Hospital. There, officers run a records check on each inmate. If no outstanding warrants turn up, the person is allowed to leave, without the strip-search.

Despite complaints from inmates and defense lawyers, the practice of releasing defendants only from the jail persisted for years. It was done to ensure that someone facing another criminal charge, here or elsewhere, was not freed by mistake.

But in 2002, several inmates challenged the practice, filing suit in federal court. The class-action suit was spearheaded by three lawyers who have been challenging the practices of local jails across the country.

In the complaint against the District, the plaintiffs contended that the "over-detention" of inmates denied them due process and that strip-searches were unreasonable.

Spurred by the public outcry over an error that caused a deaf, mute and mentally disabled man to be wrongly jailed for about two years, the Corrections Department began making improvements in its record-keeping a few years ago. That case led to its own civil lawsuit, which the city settled last month. In recent months, the outline of broader improvements took shape.

The agreement between the plaintiffs and the city cements a number of those changes and assures that the money needed to make them will be available. A fund of about $3 million, part of the $12 million settlement, will help pay for structural and technological improvements the Corrections Department is undertaking to speed up inmate releases and comply with the agreement.

The settlement sets aside $5 million for the plaintiffs, minus the cost of distributing the money. An additional $4 million will go for legal fees incurred by lawyers William C. C. Claiborne III, Lynn Cunningham and Barrett S. Litt.

Along with six plaintiffs named in the suit, as many as 20,000 people could be part of the class of ex-inmates eligible for a share of the $5 million.

Although the individual amounts could be modest, the settlement was welcome news for the people who endured the frustration and humiliation of being returned to the jail unnecessarily.

It was simply accepted, said Julian Ford, 39, of the District, one of the named plaintiffs. "They've been doing it so long, guys get used to it. What are you going to do?"

Ford was kept in the jail for two days in 2002 despite a judge's order that he be released on his personal promise to appear in court. He said the drug charge against him was later dismissed. The settlement, he said, is an important step toward helping inmates who are too ignorant or too scared to speak up. "A lot of guys don't make a lot of noise," he said. "So it's a good thing."

U.S. District Judge Royce C. Lamberth, who has been overseeing the case, gave preliminary approval to the settlement yesterday, and he applauded the plaintiffs and the government for coming to such a constructive agreement.

It is, he said, a "win for everybody," and he appeared especially pleased with the city's effort to avoid the sort of protracted litigation that for many years defined the Department of Corrections.

"It's a progressive step by the District of Columbia," Lamberth said, "and it's something the court appreciates."

Afterward, Claiborne, one of the plaintiffs' attorneys, said both sides wanted to solve the problems and worked hard toward reaching that goal. "We spent as much effort crafting the details of the settlement as we did litigating the merits," Claiborne said.

Senior Assistant Attorney General Maria C. Amato, who represented the city, said the objective was not a win but a "constructive" outcome that protected the interests of the city and its people.

"We have an obligation to the citizens of the District of Columbia," she said, "to uphold the Constitution and to do the right thing."