In the end, Mr. Ortiz appeared to have become suicidal, the authorities said. As officers were closing in, he ran to the third floor of a hotel parking garage and tried to use a parked pickup truck as a shield. He flashed his phone at the officers surrounding him, the authorities said, and they concluded he was trying to make it appear that he was holding a weapon.

Image Juan David Ortiz Credit... Webb County Sheriff’s Office, via Associated Press

“Suicide by cop,” explained Federico Garza, the chief deputy for the Webb County Sheriff’s Office.

Mr. Ortiz was taken into custody without any shots fired, and subsequently admitted to the killings and the attack on the woman who escaped, the authorities said.

Deputy Garza was asked if he was confident that investigators would not find more victims. “We know that it’s still an ongoing investigation,” he said, “and we will do historical research everywhere that he has been to see if we have a pattern of victims such as this.”

A city of 260,000 people about 145 miles southwest of San Antonio, Laredo is the busiest truck crossing on the Mexican border, but it sees less undocumented immigrant traffic than do McAllen and Brownsville, further southeast. Restaurant chains and strip malls line the roadways. Scores of colonias — small, unincorporated communities of substandard housing that often lack basic services — dot the outskirts of the city.

Mr. Ortiz’s arrest has thrust Laredo into the national spotlight, exposed its underbelly and tested the relationship between residents and the thousands of Border Patrol agents who live and work in South Texas. In large swaths of the nearby brush, Border Patrol agents are some of the only visible symbols of the law, and in the urban heart of Laredo and other cities, President Trump’s focus on border enforcement has given them expanded political clout.

Officials said that they do not believe Mr. Ortiz attacked the women while he was on duty or used his government vehicle in committing the crimes. None of the victims were undocumented immigrants; all appeared to be American citizens from the Laredo area. But the authorities said they are looking at his service revolver as a possible murder weapon.

At least two of the bodies were found along stretches of Interstate 35, which cuts through Laredo and is one of the main traffic arteries along this part of the border. Mr. Ortiz knew every mile of Interstate 35 as part of the Border Patrol’s Highway Interdiction Team, which intercepts vehicles on the interstate suspected of engaging in drug trafficking and human smuggling.