KHOGYANI, Afghanistan — When the American military dropped the largest bomb in its arsenal on an Islamic State cave complex here in eastern Afghanistan in April, the generals justified it as part of a robust campaign to destroy the group’s local affiliate by year’s end.
Its force had been reduced to 700 fighters from 3,000, they said, and its area of operation diminished to three districts from 11.
But as the year comes to a close, the Islamic State is far from being vanquished in eastern Afghanistan, even as the group is on the run in its core territory in Iraq and Syria. It has waged brutal attacks that have displaced thousands of families and forced even some Taliban fighters, who had long controlled the mountainous terrain, to seek government protection.
The shifting dynamic has, in turn, threatened the American-backed government’s tenuous hold on the region.
And two years into the joint United States-Afghanistan operation, a clear understanding of the Islamic State affiliate, the latest enemy in the long Afghan war, still evades even some of those charged with fighting it.
Gen. John W. Nicholson Jr., the top American and NATO commander in Afghanistan, recently said that 1,400 operations and airstrikes had “removed from the battlefield” more than 1,600 Islamic State fighters since March — more than double the estimate from early in the year.
Some Afghan and other Western officials question whether those numbers are inflated, but the Americans say they are an indication that the group continues to replenish its ranks with new fighters.
Part of the reason the two-year joint operation by the United States and Afghanistan against the Islamic State has made little progress is simply that the two forces are operating in a terrain where they have had little control for years. Airstrikes and commando operations bring bursts of pressure, but the militants have release valves all around them. On one side is the porous border with Pakistan, where many of the fighters come from. Elsewhere is largely Taliban country.
“It’s like a balloon,” General Nicholson said. “We squeeze them in this area, and they’ll try to move out elsewhere.”
A visit this month to Khogyani, a district in the east where Islamic State fighters have shifted, showed the increasing complexity of the Afghan conflict, and underlined how daunting a task it will be to defeat the Islamic State, also known as ISIS.
The Afghan government’s authority in Khogyani, in a remote region of Nangarhar Province, has long been confined to the district compound and the immediate surroundings. The Taliban ruled the rest. Opium has been grown all around.
After years of war with no clear victor, the region had settled into a strange sort of calm as the Taliban and the government found ways to coexist, as has happened to varying degrees around the country.
Although the Taliban are known for their opposition to girls’ education, in Khogyani, the militants here allowed schooling, showing a willingness to drop a demand that had lost them hearts and minds before. In return for having nominal control, the government has paid the salaries of teachers and health workers that the Taliban could not afford.
Abdul Jabar, who had been displaced from his home near the Pakistan border by recent battles between ISIS and the Taliban, said there was a two-story high school for girls close to his new home, with an enrollment of up to 1,600. Mir Haidar, who distributes vaccinations, said the three clinics there employed women, despite the Taliban’s past opposition to women in the workplace.
So established was Taliban rule in Khogyani that when Islamic State fighters started shifting there, many people said they trusted in the Taliban’s protection.
Then the Taliban lines started to buckle.
“The Taliban were puffing their chest — ‘We are strong, don’t go, we will take care of you,’ ” said Abdul Qadeem, a father of 13. “Then,” he said, the Islamic State “arrived at night, and we left with nothing but this shawl on my shoulder.”
The Islamic State’s local affiliate in Afghanistan first emerged in 2014, swiftly gaining ground across Nangarhar Province. It quickly drew the attention of the United States military, which had scaled down its presence in Afghanistan to a small counterterrorism mission against Al Qaeda and a larger NATO mission to train Afghan forces to hold their ground against the Taliban.
American and Afghan officials now have little reason to believe that the Afghan group, despite pledging allegiance to ISIS, maintains regular contact or receives directions from the Islamic State operating in Iraq and Syria. Instead, they say, the Islamic State in Afghanistan is largely made up of Pakistani militants pushed across the border by military operations in that country.
The militants used the Afghan mountains simply as a safe haven at first, before embracing the Islamic State and turning their weapons on Afghanistan. Opinions are divided on how and why.
Some officials believe it was only a matter of time before extremists seeking relevance would be attracted to the Islamic State. Others say the Afghan and American militaries miscalculated and fostered a new enemy by going after Pakistani militants seeking safe havens in Afghanistan in the hopes that the Pakistanis would reciprocate with Afghan Taliban leaders on their soil.
A third theory blames the cynical designs of Pakistan’s intelligence agency, which Afghans have long accused of trying to destabilize their government.
No matter who they are, the militants have brought more violence and suffering to Afghan civilians. In Khogyani, American airstrikes intended to destroy the Islamic State may have temporarily depleted its forces, but they have also upended the relative peace.
Afghan officials say they believe the fighting between the Islamic State and the Taliban has little to do with ideology.
“The reason they are fighting each other is over resources, and over territory,” said Mohammed Gulab Mangal, the governor of Nangarhar Province, who says both groups “drink from the same spring” — a subtle reference to their perceived Pakistani origins.
People in Khogyani say the Islamic State militants are better armed and fight harder than the Taliban.
“If you tie a Taliban fighter to the trunk of this tree, and then you tell him ISIS is coming, he will run so hard that he will uproot the tree with him,” said Malik Makee, a tribal elder who runs a militia of several dozen men in support of the government, helping to maintain a buffer around the district center.
Mr. Makee is an example of how fluid alliances can be here. He lived under Taliban rule for years, without much problem, until they killed one of his sons. He retaliated by killing six Taliban, and then packing up to join the government.
Mr. Makee — who admits to growing opium, as many in the district do — is one of the three men running the war in Khogyani on behalf of the government. All three are veterans of many previous conflicts.
The busiest of them these days is a potbellied intelligence operative in his 50s who is trying to peel away Taliban commanders and foot soldiers fleeing the Islamic State. His agents make a simple argument to potential defectors: You have no escape from the Islamic State, so come to us for protection. About three dozen have.
On a recent afternoon, the operative completed a deal with two Taliban over tea and raisins in a dark room of the district compound. The men, both of whom fought the government for six years, were disarmed of a rocket-propelled grenade, a Kalashnikov and a pistol.
When the operative asked if the two Taliban had government IDs, they pulled them out, neatly wrapped in plastic sheets. Asked how they had obtained the documents, one of the men, who gave his name as Zabihullah Ghorzang, replied:
“My uncle was the district governor here.”
Putting his hand on Mr. Ghorzang’s knee, the operative said the men’s decision to join the government’s side was patriotic. The government had forgiven their wrongdoings. The deal complete, Mr. Ghorzang was given back his pistol.
But Mr. Makee, who was at the meeting, couldn’t keep quiet. “The government can forgive you for all it wants,” he told the two Taliban. “But if I find out that you had done anything wrong against my men, I swear I will chase you wherever you hide.”
Lt. Col. Noor Agha, the third of the three men fighting ISIS here, is a district police chief who cuts a soft-spoken, fatherly figure. He has been in charge of the last line of defense against ISIS: This far into Taliban country, there were no Afghan commandos or American special forces to help out.
Colonel Agha has only about 170 regular police officers under his command, 30 of them not present for duty, and about as many militiamen, such as the ones Mr. Makee commands.
As residents started fleeing last month from the fighting between the Islamic State and the Taliban, it suddenly appeared that the government could lose even its nominal control if ISIS fighters reached a strategic hill.
Colonel Agha carved a road from the district center to the top of the hill, a path made for the first time, 16 years into the war. He cobbled together about 50 men, 20 of them from the army, and they made their way through the poppy fields to establish outposts on the hill.
Many of the men on the hill slept under the open sky. They lacked heavy weaponry. As they looked down, it was Taliban to the right, Taliban to the left, Taliban at the foot of the hill. In the distance was the Islamic State, its fighters heavily armed with machine guns and rocket launchers, Col. Agha says. He knows that because his troops have been fired upon and taunted over the radio as infidels and American militias.
Letting down his pride for a moment, the colonel admitted they couldn’t fire back.
“What can we hit them back with?”
In the end, Colonel Agha’s fighters were saved from having to find out by additional Afghan Army units, who arrived to help clear ISIS from Khogyani. Local commanders in recent days say they have killed at least 50, and the rest have started shifting their base of operations — again.