Brad Lander, the New York City comptroller who is running for mayor, was arrested on Tuesday by federal agents at an immigration courthouse in Lower Manhattan as he tried to escort a migrant whom agents were seeking to arrest.

Mr. Lander, a Democrat, was observing proceedings at the city’s main immigration courthouse, at 26 Federal Plaza, where an increasing number of migrants who appear for court have been arrested in recent weeks by Immigration and Customs Enforcement agents. A spokeswoman for the Department of Homeland Security later said that Mr. Lander had assaulted and impeded a law enforcement officer, though as of Tuesday afternoon, Mr. Lander had not been formally charged.

Videos taken by reporters at the courthouse show Mr. Lander standing by a migrant man in a hallway on the 12th floor when several men in plainclothes who appear to be law enforcement officers, some wearing masks, push past a crowd in the hallway to arrest the migrant.

Mr. Lander repeatedly asks the agents whether they have a judicial warrant, refusing to let go of the migrant as the agents seek to shepherd the man toward the elevators, according to one of the videos, which was posted on social media by a reporter from The City, a digital news outlet.

“I will let go when you show me the judicial warrant,” Mr. Lander says, his right hand on the man’s body.

Agents can be seen trying to pry Mr. Lander away. They ultimately separate him from the man, push him against a wall by the elevators and place handcuffs on him.

“You don’t have the authority to arrest U.S. citizens,” Mr. Lander says repeatedly, according to the video. “I’m not obstructing. I’m standing right here in the hallway. I asked to see the judicial warrant.” (Agents do not need judicial warrants to make arrests in immigration courts because they are public spaces, immigration lawyers said.)

The agents walked Mr. Lander into an elevator in the building, accompanied by a member of his security detail. Dora Pekec, a spokeswoman for Mr. Lander, said that he “was taken by masked agents and detained by ICE.”

Mr. Lander was held at 26 Federal Plaza for several hours before being released on Tuesday afternoon, as Gov. Kathy Hochul and other Democrats showed up at the federal offices demanding his release.

At a news conference, Mr. Lander rejected claims that his arrest had been a premeditated publicity stunt and denied that he had assaulted anyone, saying, “Seriously?”

“I did not come today expecting to be arrested,” Mr. Lander said. “But I really think I failed today because my goal was really to get Edgardo out of the building,” he added, referring to the migrant ICE had detained.

In a statement, Tricia McLaughlin, a spokeswoman for the Homeland Security Department, the parent agency of ICE, said that Mr. Lander “was arrested for assaulting law enforcement and impeding a federal officer.”

“Our heroic ICE law enforcement officers face a 413 percent increase in assaults against them,” she added. “It is wrong that politicians seeking higher office undermine law enforcement safety to get a viral moment. No one is above the law, and if you lay a hand on a law enforcement officer, you will face consequences.”

A spokesman for the Manhattan U.S. attorney’s office said in a statement that the office was investigating Mr. Lander’s actions, but said nothing about criminal charges. The spokesman, Nicholas Biase, noted that federal law prohibited assaults on law enforcement and other public officials and obstruction of official proceedings.

Mr. Lander’s arrest was the latest in a series of arrests of Democratic elected officials by federal agents as Democrats scramble to push back against President Trump’s deportation campaign.

It came five days after Senator Alex Padilla, Democrat of California was shoved out of a room and handcuffed by federal officers after he had tried to question Kristi Noem, the homeland security secretary, during a news conference.

Last month, federal agents arrested Ras J. Baraka, the mayor of Newark, and later charged Representative LaMonica McIver, both Democrats, in connection with a volatile clash outside an immigration detention center in the city.