WASHINGTON — Five transgender people serving in the United States military sued President Trump and top Pentagon officials on Wednesday, asking that transgender troops be allowed to stay in the military.

The lawsuit was filed in response to Mr. Trump’s ban abruptly announced last month on Twitter.

The plaintiffs filed the lawsuit under pseudonyms — “Jane Doe” Nos. 1-5 — in the United States District Court for the District of Columbia. The case was organized by two rights groups, the National Center for Lesbian Rights and GLBTQ Legal Advocates & Defenders, or GLAD.

Other rights groups — like Lambda, Outserve and the American Civil Liberties Union — have also said they are preparing lawsuits but are holding off until the Trump administration takes a step to put the ban into effect, such as issuing formal guidance to the military or beginning the process of changing military rules.

But Shannon Minter, the legal director for the National Center for Lesbian Rights, said he believed the case was already ripe for a lawsuit because active transgender service members — such as those deciding whether to re-enlist — were already being harmed by the uncertainty created by Mr. Trump’s statements on Twitter.