PHOENIX — Border Patrol agents pursue smugglers one moment and sit around in boredom the next. It was during one of the lulls that Bryan Gonzalez, a young agent, made some comments to a colleague that cost him his career.

Stationed in Deming, N.M., Mr. Gonzalez was in his green-and-white Border Patrol vehicle just a few feet from the international boundary when he pulled up next to a fellow agent to chat about the frustrations of the job. If marijuana were legalized, Mr. Gonzalez acknowledges saying, the drug-related violence across the border in Mexico would cease. He then brought up an organization called Law Enforcement Against Prohibition that favors ending the war on drugs.

Those remarks, along with others expressing sympathy for illegal immigrants from Mexico, were passed along to the Border Patrol headquarters in Washington. After an investigation, a termination letter arrived that said Mr. Gonzalez held “personal views that were contrary to core characteristics of Border Patrol Agents, which are patriotism, dedication and esprit de corps.”

After his dismissal, Mr. Gonzalez joined a group even more exclusive than the Border Patrol: law enforcement officials who have lost their jobs for questioning the war on drugs and are fighting back in the courts.