Instead, the Islamic State fighters fled on foot to government lines, but more could have escaped the Taliban pursuit, Mr. Nemat said. “We lost contact with the others last night. I think they contacted the Taliban or went somewhere else.”

Among his followers, he said, were about 25 to 30 foreigners, mostly from Central Asian countries, but also two Frenchmen, known only by Arabic pseudonyms, Abu Mohammad and Abu Mariam.

In addition to Mr. Nemat, who is also known as Mufti Nematullah Qaweem, the other Islamic State military commander, Maulavi Habib ul-Rahman, surrendered to the Afghan government forces, according to Abdul Hafiz Khashi, the deputy police chief of Jowzjan Province. The two leaders are brothers-in-law.

“The areas the Daeshis controlled were taken by the Taliban,” Mr. Khashi said.

The police chief of Darzab, Capt. Mohammad Ismail Mubarez, said the Taliban had forced the Islamic State fighters to surrender to the government side. “Two hundred of them surrendered,” he said. “They came at four in the morning with their weapons.”

The defeat was the latest in a series of setbacks for the Islamic State in Khorasan, as the group’s affiliate in Afghanistan calls itself. Since last year, their positions in southern Nangarhar Province have been battered by American airstrikes and ground attacks from American and Afghan Special Forces.

The group has reacted in Nangarhar Province by concentrating its attacks on civilian “soft targets,” most recently on Tuesday, with an attack on a refugee agency. The Afghan government responded to that assault by declaring that the army would take charge of security in Jalalabad, the capital of Nangarhar Province, from Wednesday.

In northern Afghanistan, Mr. Nemat was a former Taliban leader who had switched sides to the government in a deal brokered by the country’s first vice president, Gen. Abdul Rashid Dostum. Last year, Mr. Nemat switched sides again, this time to the Islamic State. Initially, he was successful, overrunning government positions in Darzab, and the group gained traction elsewhere in the north as well.