In recent years, American advisers helped the Afghans set up a separate police agency and court for drug dealers. The goal was to insulate narcotics prosecutions from the rampant corruption elsewhere. So far, American officials say, the approach appears to be working, although it is unclear whether it will have any long-term effect on drug trafficking here, which is soaring.

The agency, the Counter Narcotics Police of Afghanistan, captured Mr. Ishaqzai after a shootout in 2012.

In the 1980s, Mr. Ishaqzai, 61, began buying and selling opium paste, and in the 1990s he built labs to refine the paste into heroin. When the Taliban were toppled in late 2001, Mr. Ishaqzai moved from Sangin, in Helmand Province, to Kandahar, the most important city in the south. There he found a protector in the regional strongman, Ahmed Wali Karzai, the half brother of Hamid Karzai, then the president. Mr. Ishaqzai and his new ally lived on the same street and often played cards together.

Mr. Ishaqzai’s situation became precarious after Mr. Karzai was killed in 2011. That year President Obama added his name to a list of foreign drug kingpins. Then Mr. Ishaqzai was arrested by the counternarcotics police. While he was in custody, ministers sought his release but were rebuffed, a Western counternarcotics official said.

He was sentenced to 20 years in prison, prompting officials from the State and Justice Departments in Washington to cite the case as an encouraging sign. But Mr. Ishaqzai was already working toward his freedom. Investigators looking into his escape were stunned to learn that Mr. Ishaqzai’s release was arranged with the proper paperwork and the backing of judges. Officials said about 10 people — mostly judges, but also prison officials, a court clerk and others — are suspected of having a hand in releasing Mr. Ishaqzai.

In April, Mr. Ishaqzai was transferred out of the nation’s largest prison, in Kabul, to a smaller one in Kandahar on the order of the Supreme Court and the Interior Ministry. In June, two lower-ranking judges signed a release order for him.

The transfer and release orders occurred under provisions of the penal code that allow judges to exercise leniency under certain conditions, though the law was changed just days later.