The District government has agreed to pay up to $6.2 million to resolve allegations in a class-action lawsuit that it detained inmates beyond their court-ordered release dates and subjected them to unnecessary strip searches.

The agreement, filed this week, was reached after seven years of litigation and months of “extensive, often contentious, negotiations,” according to the proposed settlement, which must be approved by the court.

In 2011, U.S. District Judge Royce C. Lamberth found that the District government had violated the rights of hundreds of inmates by detaining and strip searching them long after they had received orders for release.

In the past, some inmates were held for days, sometimes months, after they were ordered to be released. But the court found in 2008 that the Corrections Department had taken significant steps to address a historical problem.

A jury in March also sided with the government, finding that the D.C. jail did not show deliberate indifference in keeping individuals beyond their court-ordered release dates.

William Claiborne, the lead attorney in the lawsuit, acknowledged that the District has improved its release system. Claiborne said he believes that most defendants who go to court and are entitled to release are now released at the courthouse instead of being returned to jail.

If approved, the settlement calls for the distribution of $2.9 million to former defendants who file valid claims, and $75,000 to the named plaintiffs. In addition, the city government would pay $475,000 to improve inmate processing at the Department of Corrections. The remainder of the $6.2 million would cover legal and administrative costs.

A spokesman for the District’s Attorney General declined to comment on the preliminary agreement.