Nearly six months after his escape from a maximum-security prison in Mexico, the drug kingpin Joaquín Guzmán Loera, also known as El Chapo, has been arrested by the Mexican authorities, President Enrique Peña Nieto announced Friday.

The arrest came after an intense gun battle Friday morning in the city of Los Mochis, a seaside area in Mr. Guzmán’s home state of Sinaloa.

“Mission Accomplished: We have him,” read the announcement from Mr. Peña Nieto. “I would like to inform the Mexican people that Joaquín Guzmán Loera has been detained.”

The mission began shortly before 5 a.m. Friday, the Mexican authorities said, after an anonymous tip came in from a citizen concerned about armed men in a nearby home.

The authorities said they went to the house, where they were fired upon. The operation was conducted by Mexico’s most-trusted military wing, the Marines, which captured Mr. Guzmán in early 2014, before his escape last July.

It is unclear whether the government knew Mr. Guzmán was in Los Mochis, or whether his capture was a fortunate coincidence. Another leader of Mr. Guzmán’s Sinaloa cartel managed to escape, the authorities said, in the first indication that the gun battle involved high-ranking members of the cartel.

The capture of the fugitive drug lord concludes a deeply embarrassing chapter for the government of Mr. Peña Nieto, which has been waylaid by a series of security and corruption scandals that reached their low point with Mr. Guzmán’s daring escape.

Now, a looming question is whether the Mexican authorities will try to hold Mr. Guzmán for a third time  he has already escaped from prison twice — or whether they will hand him over to the Americans.

Mr. Guzmán stunned the world last summer when he stepped into the shower in his cell  in the most secure wing of one of the most secure prisons in Mexico — and abruptly vanished in full view of a video camera. When guards entered the cell, they discovered a small hole in the shower floor, through which Mr. Guzmán had disappeared.

The opening in the shower led to a mile-long tunnel to a construction site. The tunnel was more than two feet wide and more than five feet high, tall enough for Mr. Guzmán to walk through standing upright, his nickname translates to Shorty and was burrowed more than 30 feet underground.

NYT