The lawsuit revealed that the Department of Homeland Security and US Customs and Border Protection had sought the unmasking of the @ALT_USCIS account holder who has been criticizing President Donald Trump's administration.
The suit could portend a contentious battle between the social network and the US administration over efforts to crack down on government leaks.
The account in question is purportedly run by one or more current employees of the US Citizenship and Immigration Services (USCIS), a unit within Homeland Security.
It is one of several "alternative" handles apparently created by government workers after Trump's inauguration in January that have sometimes revealed data the new administration sought to suppress or remove from official websites.
"The rights of free speech afforded Twitter's users and Twitter itself under the First Amendment of the US Constitution include a right to disseminate such anonymous or pseudonymous political speech," the lawsuit says.
It was filed in a California federal court.
The government "may not compel Twitter to disclose information regarding the real identities of these users without first demonstrating that some criminal or civil offense has been committed," it said.
The so-called social media resistance movement began after the deletion of tweets and data from official US accounts and websites that proved embarrassing to the new president, including government reports about climate change.
After some took to Twitter with "alternative" handles -- claiming to be federal employees exercising their free-speech rights -- the resistance mushroomed into a movement.
Twitter said it received a summons on March 14 to produce the name or names of the account holder, saying the document asked the company "not to disclose the existence of this summons for an indefinite period of time."
The company said it contacted the agent who delivered the summons, who "stated vaguely that he is conducting an investigation" but "did not identify any law or laws that he believed had been broken."
- Fear of retribution -
Twitter's lawsuit noted that "those who speak through these 'alternative agency' accounts do so pseudonymously, often going to considerable lengths to avoid disclosing their real identities" for fear of retribution.
"Such fears are likely to be especially great for users of 'alternative agency' accounts who are currently employed by the very agency that is a principal target of the commentary, in light of the retaliation, harassment, or even loss of livelihood that might occur if their real identities became known to their superiors," the complaint said.
Twitter said the government agencies, in seeking the identities of the account holders, would be "unlawfully abusing a limited-purpose investigatory tool," and asked the court to invalidate any summons for the data.
The American Civil Liberties Union praised Twitter's action, tweeting: "We're glad Twitter is pushing back. We'll be going to court to defend this user's right to anonymous speech."
The US Justice Department declined to comment on the case. Twitter had no additional comment beyond the lawsuit.