MEXICO CITY — As the most notorious drug trafficker this country has ever produced, Joaquín Guzmán Loera knew he had a story worthy of Hollywood gold.

And that, in part, led to his undoing. The uneducated son of a farmer who became a cartel kingpin, married a beauty queen wife and made two Houdini-like escapes from prison, Mr. Guzmán, known as El Chapo, wanted to make his own biopic.

After his escape from a maximum-security prison six months ago, he instructed his people to get in touch with actresses and producers. But his rags-to-riches film ambitions quickly turned into a flop. The government used the contacts between his people and those in the film industry to open a new line of investigation, according to Arely Gómez González, Mexico[1]'s attorney general, who gave a lengthy statement on the arrest late Friday night.

Early Saturday, reporters swarmed the coastal city of Los Mochis where Mr. Guzmán was apprehended[2]. The local authorities promised to share more details later in the day, as well as to give a tour of the home where the gunfight took place.

Mexico is considering extraditing Joaquín Guzmán Loera, or El Chapo, to the United States, officials said.

The government has said little publicly about whether it will send Mr. Guzmán to the United States, where he faces drug trafficking and murder charges. He has twice escaped from Mexican custody, a deep embarrassment for the country. A debate is emerging among those in favor of extradition on whether to fast-track his passage to the United States or to go through the normal process.

For now, however, euphoria is evident in the government of President Enrique Peña Nieto. Shortly after his top security official announced the news at a meeting of diplomatic staff, the crowd broke into a rendition of the national anthem.

The manhunt for Mr. Guzmán reached a finale in the early hours of Friday, when Mexican marines stormed a compound in Los Mochis where he had holed up with several others. During the ensuing firefight, which raged for more than an hour, Mr. Guzmán managed to escape with a top lieutenant through a storm drain, as authorities said they had expected him to do.

The men emerged through a manhole, and then stole a car. But the authorities quickly tracked them down, capturing them along the highway from Los Mochis to Navojoa. Mr. Guzmán was taken to Mexico City and, late on Friday night, paraded before the cameras wearing blue track pants and his signature mustache, en route to the same prison he escaped from in July[3], the maximum security facility Altiplano.

That episode helped burnish Mr. Guzmán's reputation as an escape artist. It dominated headlines in Mexico for weeks, as the public marveled at both his skills and the government's ineptitude.

With an estimated wealth of nearly $1 billion and Mexico's most powerful cartel behind him, Mr. Guzmán was the most well-financed fugitive in history. Few thought the government would be able to catch him again.

But the government was engaged in what may have been its most extensive manhunt ever. Investigators focused on the rugged mountains of northwest Mexico, an area called the Golden Triangle, along the borders of the states of Sinaloa, Durango and Chihuahua. They identified Mr. Guzmán's logistics network, which included airplanes, runways and a range of property and vehicles.

For months, the circle was tightening around Mr. Guzmán, Ms. Gómez said.

The search began with the investigation into his escape, as the police rounded up the people who built the tunnel and flew Mr. Guzmán to a hiding place in the northwestern mountains. They tracked him to a ranch in the remote municipality of Pueblo Nuevo in Durango.

By October, they were ready to act. Special forces units aboard a helicopter had him in their sights, but they could not shoot; Mr. Guzmán was accompanied by two women and a girl, and the snipers held their fire.

As he fled, Mr. Guzmán slipped and fell, injuring his leg and face, the authorities said at the time. Though he escaped, security forces were able to capture seven of his men, and those arrests led to more intelligence about his movements.

Mr. Guzmán retreated farther into the mountains, cut back his guard, and limited his communications, Ms. Gómez said. But by that time, Mexican intelligence was tracking his moves, helped in part by the feelers he had put out about the movie.

By the end of December, the authorities became aware that Mr. Guzmán would be traveling through a city. The government began tracking one of Mr. Guzmán's chief tunnel architects, who was preparing different homes in the states of Sinaloa and Sonora for his boss's arrival.

Among those homes being watched was the one in Los Mochis, a city of some 250,000 near the Sinaloan coast known for producing some of the country's best boxers. After nearly a month of surveillance, on Wednesday, the authorities noticed unusual activity inside the house. At dawn on Thursday, a car appeared. Based on intelligence, the authorities said, they knew that Mr. Guzmán was inside.

At dawn on Friday, federal forces began a raid on the house. They were met with high-caliber gunfire. Mr. Guzmán, for his part, reverted to an old playbook: He fled.

Hours later, the Mexican president, using his Twitter account[4], announced the news of his capture.