MEXICO CITY — At least a dozen armed men staged an attack Friday on Mexico City’s top security official, who narrowly escaped the apparent assassination attempt that left at least three people dead.

The gunmen shot .50-caliber weapons at the SUV carrying Omar García Harfuch, Mexico City’s secretary of public security, in one of the capital’s most exclusive neighborhoods. García Harfuch was injured in the attack but was in stable condition. Two police officers and one passerby were killed, officials said. Twelve people were detained.

Violence has continued to rise across Mexico, even during the coronavirus pandemic. Mexico City’s wealthier enclaves have typically been spared, though, making Friday’s attack particularly startling for residents here. Photos of García Harfuch’s Chevrolet Suburban show the vehicle ripped apart by bullets.

Several hours after the attack, García Harfuch wrote on Twitter that one of Mexico’s largest drug cartels, the Jalisco New Generation Cartel, known by its Spanish acronym, CJNG, was responsible for the attack.

“This morning we were cowardly attacked by the CJNG, two colleagues and friends of mine lost their life, I have three bullet wounds and several shards,” he wrote. “Our Nation must continue to confront the cowardly organized crime. We will continue working.”

The Jalisco cartel has risen to prominence since splitting off from the Sinaloa Cartel around 2010. It has gained territorial control over much of Mexico and has violently clashed with other cartels during that expansion. But it has seldom attempted to assassinate high-level Mexican officials.

The United States has actively targeted members of the cartel, arresting hundreds of people associated with the group and extraditing others.

At 38, García Harfuch is among the youngest people ever to ascend to the top of Mexico City’s security apparatus. Many see him as a promising leader, willing to take on the country’s growing problem of narco-violence.

On Friday afternoon, the Mexico City attorney general’s office interrogated suspects. In a statement, Attorney General Ernestina Godoy Ramos said detainees admitted that a total of 28 gunmen were recruited about three weeks ago and were promised cash in exchange for their participation.

After the attack, Mexican President Andrés Manuel López Obrador suggested that it was García Harfuch’s willingness to take on organized crime that made him a target.

“It has to do, without a doubt, with the work that is being carried out to guarantee peace and tranquility both in Mexico City and in the country,” López Obrador said at a morning news conference.

Earlier this week, six police officers were ambushed and killed in the state of Guerrero. Last weekend, 12 people were killed in the state of Sonora during what officials described as a clash between two rival criminal groups.

Gabriela Martinez in Mexico City contributed to this report.