The plaintiffs had argued that Mr. Trump’s Twitter feed is an official government account and that blocking users from following it is a violation of their First Amendment rights.

In June, the plaintiffs had sent a letter to the White House asking to be unblocked. When that went ignored, they sued.

Their complaint argued that Mr. Trump’s feed amounted to a “digital town hall” where not only did the president and his aides communicate information but members of the public — by replying to Mr. Trump’s tweets and others who responded to him — exchanged views with one another. By blocking particular people from viewing or replying to message chains because they had expressed views he did not like, it argued, Mr. Trump had violated their First Amendment rights.

Judge Buchwald, who was appointed to the federal bench in 1999 by President Bill Clinton, agreed.

“The viewpoint-based exclusion of the individual plaintiffs from that designated public forum is proscribed by the First Amendment and cannot be justified by the president’s personal First Amendment interests,” she wrote.

The ruling also rejected the government’s claim that Mr. Trump operates the account merely in a personal capacity, concluding that he “uses the account to take actions that can be taken only by the president as president.”

Judge Buchwald, however, did dismiss Sarah Huckabee Sanders, the White House press secretary, and Hope Hicks, the former communications director, as defendants because Ms. Sanders does not have access to the account and Ms. Hicks no longer works in the administration.

Mr. Jaffer said Mr. Trump or Mr. Scavino should simply unblock the users. If they do not, the next step could be to seek an injunction. (Judge Buchwald mentioned that possibility, writing that “injunctive relief directed at Scavino” was one of the options that “remain available.”)