A Mexican judge said Thursday that he revoked bail for an alleged drug cartel member just weeks before the man allegedly shot a U.S. immigration agent to death and wounded another.

The judge said he had originally granted the suspect a form of bail in another case because the most serious charges against him did not hold up.

Suspect Julian Zapata Espinoza was detained Wednesday by the Mexican army. The army said he confessed to killing U.S. Immigration and Customs Enforcement agent Jaime Zapata on a highway near the northern city of San Luis Potosi on Feb. 15, in what he claimed was a case of mistaken identity.

Zapata Espinoza had been arrested in 2009 on charges of illegal weapons possession, organized crime and possession of counterfeit police insignia and using a stolen car. He was released and later jumped bail.

District court judge Juan de Dios Monreal explained the bail ruling Thursday, saying he had to dismiss the only two charges under which he could have denied bail for Zapata Espinoza because of a lack of evidence.

Zapata Espinoza was shown in detention photos in 2009 with a group of other suspects and guns and ammunition that had allegedly been seized from them in a raid by the army. The remaining charges were for lesser forms of weapons possession and use of police insignia that were eligible for bail.

Since that arrest in 2009, there were indications that Zapata Espinoza was a local leader of the violent Zetas drug cartel, Mexican authorities have said. His bail was revoked Jan. 18.

But officials have had a hard time making the charges stick, and even after his detention in the ICE shooting, Zapata Espinoza had not been charged in that case. He was being held under a form of house arrest that prosecutors in Mexico frequently use to gain time to build a case.

Zapata Espinoza — known by the nickname "El Piolin," or Tweety Bird, apparently because of his short stature — allegedly told soldiers after his arrest that a group of gunmen from the Zetas mistook the ICE agents' SUV for one used by a rival gang.

"That event occurred because of the characteristics of the vehicle, given that they (the suspects) thought it was being used by members of a rival criminal group," an army spokesman, Col. Ricardo Trevilla, said Wednesday night.

The two agents were in a Chevrolet Suburban. Mexico's drug cartels frequently set up roadblocks and ambushes to steal large SUVs and pickups.

Zapata Espinoza and five other men detained with him were presented to journalists Wednesday night. The army quoted Zapata Espinoza as saying two of the five had participated in the attack on the ICE agents: Jesus Ivan Quezada, alias "El Loco," and Honduran Ruben Dario Venegas.

Agents Jaime Zapata and Victor Avila, who worked at the U.S. Embassy, were attacked as they returned to Mexico City from a meeting with other U.S. personnel in the state of San Luis Potosi. Zapata was killed, while Avila suffered leg wounds and is recovering in the United States.

San Luis Potosi is at the center of a power struggle between two rival drug gangs, the Zetas and the Gulf cartel. It is also on the route north used by migrants seeking to reach the United States, and officials say cartels have begun recruiting some migrants to work for the gangs.

Last week, some U.S. officials maintained the attack was an intentional ambush of the agents and said the gunmen made comments before they fired indicating they knew who their targets were.

But although Mexico is seeing record rates of violence, it is rare for U.S. officials to be attacked, and mistaken identity has resulted in other killings.

One of the most notorious occurred in 1993 when gunmen linked to the Arellano Felix drug cartel killed Roman Catholic Cardinal Juan Jesus Posadas Ocampo at an airport in the western city of Guadalajara. Prosecutors later said the cardinal's luxury card led the gunmen to mistake him for their intended target, drug lord Joaquin "El Chapo Guzman."

A slaying in September — of American tourist David Hartley — was also listed by law enforcement officials as a case of mistaken identity.