WASHINGTON—Federal officials accused Facebook Inc. Thursday of unlawful discrimination by allowing real-estate companies to target potential customers by race, religion and other factors, and signaled that other online advertising platforms are in its crosshairs.

The Department of Housing and Urban Development said the social-media giant violated the Fair Housing Act “by restricting who can view housing-related ads.”

HUD has sent letters to a number of technology companies, including Alphabet Inc. unit Google and Twitter Inc., asking for more information about their sophisticated advertising systems, an agency official said. Those inquiries are preliminary and don’t amount to formal investigations, but could lead to additional probes.

“Just because a process to deliver advertising is opaque and complex doesn’t mean that it exempts Facebook and others from our scrutiny and the law of the land,” HUD General Counsel Paul Compton said in a statement. “Fashioning appropriate remedies and the rules of the road for today’s technology as it impacts housing are a priority for HUD.”

A spokesman for Facebook said the company was surprised by HUD’s action, saying it had been working with the department to address the agency’s concerns. The spokesman said the company last year eliminated thousands of targeting options subject to misuse, among other measures.