U.S. Atty. Gen. Loretta Lynch personally announced the lawsuit, which argues that North Carolina's so-called bathroom law violates parts of the Civil Rights Act of 1964 and other federal laws, and that the state is engaging in a "pattern or practice of sex discrimination."
Lynch stepped in hours after North Carolina's Republican governor, Pat McCrory, had sued the Justice Department to prevent it from blocking implementation of the state law, which requires public agencies to deny transgender people access to multiple-occupancy bathrooms and changing rooms consistent with their gender identity.
At a news conference, Lynch linked the dispute to past civil rights struggles over equal access to housing, water fountains and other facilities.
"This action is about a great deal more than just bathrooms," she said. This is "about the respect we accord our fellow citizens and the laws that we … have enacted to protect them."
She added, "This is not the first time we have seen discriminatory responses to historic moments of progress for our nation."
The federal lawsuit names the state of North Carolina, McCrory, the state's Department of Public Safety, the University of North Carolina system and its Board of Governors as defendants.
Since the Supreme Court last year upheld the right of same-sex couples to marry, state legislatures have taken up various bills to address the extent of rights enjoyed by gays, lesbians and transgender people.
North Carolina was the first state to pass a law that requires public bathrooms or changing facilities "be designated for and only used by individuals based on their biological sex." More than half a dozen states are weighing similar legislation.
The Justice Department had set a Monday deadline for North Carolina to explain how it would amend its law to meet federal standards. Federal officials had said the state could face a lawsuit and the potential loss of billions of dollars in federal funding for state agencies and universities.
"Denying such access to transgender individuals, whose gender identity is different from their gender assignment at birth, while affording it to similarly situated non-transgender employees violates" federal law, Vanita Gupta, who heads the department's civil rights division, wrote in a May 4 letter to the governor.
In the state's lawsuit against the department, it accuses the federal government of "baseless and blatant overreach" by trying to block the state from enforcing its law.
"This is an attempt to unilaterally rewrite long-established federal civil rights laws in a manner that is wholly inconsistent with the intent of Congress and disregards decades of statutory interpretation by the courts," says the lawsuit, which was filed in federal district court in Raleigh, the state capital.
The state law is strongly supported by social conservatives, and the dispute over gay and transgender rights may affect some down-ballot races in the 2016 election cycle.
Although Sen. Ted Cruz of Texas backed the North Carolina law, he dropped out of the Republican
"Leave it the way it is," Trump said during a recent town hall. "There have been very few complaints the way it is."
Legal experts say the Justice Department is likely to prevail in court.
They cited a recent decision by the U.S. 4th Circuit Court of Appeals, which has jurisdiction over North Carolina, that found a transgender high school student who was born as a female could sue his school board in Virginia for discrimination because it banned his use of the boys' restroom.
The appeals court ruled 2 to 1 that courts should defer to an earlier U.S. Department of Education decision that transgender students can choose a restroom that corresponds to their gender identity.
"This is a contentious legal issue," said William Yeomans, a law professor at American University and former federal civil rights lawyer. "I think the federal government's call is the right one. The fact that a panel on the 4th Circuit upheld this view suggests the direction this is headed."
Carl Tobias, a University of Richmond law professor, said the dispute could take more than a year to resolve and may reach the Supreme Court.
"North Carolina is on the cutting edge of this," Tobias said, referring to the wave of pending legislation. "And on the merits, it seems to be in violation of federal law. The Justice Department has a duty to stop discrimination in the workplace and educational institutions."
The North Carolina law, which was enacted March 23, also blocks local governments from allowing transgender people to use bathrooms that correspond to their sexual identity.
The law applies to bathrooms in state government offices and facilities, including libraries, airports, universities and schools, and highway rest stops. It does not apply to bathrooms in private businesses.
McCrory and other state Republican leaders have defended the law as necessary to protect women from sexual assault.
The state legislature passed it after the Charlotte City Council approved an ordinance in February that permitted transgender people to use a public restroom matching their gender identity.
The state law has sparked protests from gay rights groups, some companies and businesses, entertainers and sports teams.
PayPal last month canceled plans to open an operations center in North Carolina, Bruce Springsteen nixed a concert, and the NBA said it would move next year's All-Star game from Charlotte to another state if the law was not changed.
1:20 p.m.: This article has been updated to include the Obama administration lawsuit.
10:59 a.m.: This story has been updated with staff reporting.
7:41 a.m.: Updated with more information throughout.