The Justice Department asked a federal judge yesterday to find D.C. Mayor Marion Barry and other city officials in contempt of court for failing to carry out his four-year-old order substantially to improve conditions at Forest Haven, Washington's home for the mentally retarded.

The department's civil rights division also asked U.S. District Court Judge John H. Pratt to order the city to implement "major improvements at the Forest Haven facility as well as the gradual transfer of residents to. . . group homes, foster homes and the like."

Mayoral press secretary Annette Samuels declined to comment yesterday, saying Barry knew about the Justice Department's action but "had not been briefed."

In June 1978, Pratt ordered the city to overhaul procedures at the 400-bed Laurel residence. Since then, the city has been ordered three times to make improvements in sanitation, maintenance and deinstitutionalization of residents.

In yesterday's request to the judge, the Justice Department said it had found a number of continuing problems. These include water from leaking toilets, falling plaster, sleeping areas without heat, and residents who had to sleep on mattresses on the floor.

For many residents, Justice said, "the lack of needed services means that their days are spent in idleness--a situation so frequently reported during the pendency of this case that it seems to have lost its capacity to shock."

"I thought things were going okay," said Forest Haven superintendent Donald C. Brooks, who also said he knew nothing about the motion. The city's corporation counsel could not be reached for comment.