Judge Reeves said that both the United States Department of Justice, which brought the lawsuit in 2016, and the state must submit names of potential special masters within 30 days.

The Department of Justice said in an emailed statement that it was “pleased with the decision.”

“The court’s decision noted the harmful impact institutionalization has on individuals with serious mental illness including the lack of freedom, independence, and privacy,” the department said.

It is not clear whether the state intends to appeal the judge’s ruling. At a news conference Wednesday, Jim Hood, the attorney general of Mississippi, said it was too early to file an appeal. He expected that even after Judge Reeves decides on a special master, it will probably take several months before a final plan on the mental health system is put into place, and a final order from the court is issued.

Mr. Hood, who is running for governor, said he had repeatedly warned the Mississippi Legislature since 2013 about underinvesting in community mental health services.

“Many of us don’t like a federal court telling us what to do,” Mr. Hood said. “I don’t like it. We ought to fix our own problem before a federal court has to step in.”

He said in a statement that the state would “continue to work with the court to increase the mental health services available to Mississippians.”

The state’s mental health system had been scrutinized by the Justice Department for nearly a decade. The department sent the state a letter in 2011, saying it was violating the Americans with Disabilities Act by “unnecessarily institutionalizing persons with mental illness,” according to Judge Reeves’s opinion.