A spokesman for Customs and Border Protection declined to provide information about the encounter or to comment on the lawsuit, which also names Mr. O’Neal, the acting Customs and Border Protection Commissioner Kevin K. McAleenan and 25 unnamed “John Doe” defendants the A.C.L.U. said were involved in the episode.

“As a matter of policy, U.S. Customs and Border Protection does not comment on pending litigation,” Jason Givens, an agency spokesman, said in an email. “However, lack of comment should not be construed as agreement or stipulation with any of the allegations.”

Efforts to locate Mr. O’Neal on Thursday evening were not immediately successful.

In a statement, the A.C.L.U. said that it was unconstitutional for law enforcement or immigration agents to detain people because of their language, accent or race. Cody Wofsy, a lawyer with the organization’s Immigrants’ Rights Project, said he saw the case “as a part of a broader pattern of abusive conduct by an out-of-control agency.”

“This is an opportunity for the courts to step in and say there are constitutional limits on what C.B.P. can do,” he said. “This is certainly nothing new based on what we have seen over the last couple of years of the Trump administration. The agency has been emboldened to act on some of its worst impulses.”

The case highlights concern over the far-reaching power of Customs and Border Protection, whose agents have the authority to detain and question people up to 100 miles from an international border, a vast area that is home to almost two-thirds of the population of the United States.

Havre, a remote farm city of about 9,000 people, is home to a Customs and Border Protection field office with 183 agents who have jurisdiction over more than 450 miles of the Canadian border. The city is about 35 miles from the border.

In 2006, the United States Court of Appeals for the Ninth Circuit ruled that border agents illegally detained five Latino men in Havre in 2004 and said that “apparent Hispanic ethnicity, although a relevant factor in the reasonable suspicion inquiry, cannot by itself justify an investigatory stop in a border area.”