While the Taliban have been resurgent around the country, exerting increasing pressure in previously safe areas of the north, the Islamic State threat has largely remained contained. The group has been targeted by the Afghan Army and United States air power in its stronghold, the Achin district in Nangarhar province. But other, smaller pockets claiming Islamic State affiliation have also popped up in other parts of the country.

While the Taliban and fighters affiliated with the Islamic State have fought turf battles in the east, officials have long said the groups’ interactions in the north have been more complex, with the two often having a symbiotic relationship. Intelligence and security officials have expressed uncertainty over whether what operates as the Islamic State in parts of Afghanistan is really tied to the group’s headquarters in Syria and Iraq at all, or if it is just the Taliban and other militant groups with a new identity.

“One of the things we are concerned about here in Afghanistan, the reason we think that the entire world needs to be focused on Afghanistan, is the potential for convergence among the various terrorist groups in this area,” Gen. John W. Nicholson Jr., the commander of NATO and American forces in Afghanistan, said at a news conference in April.

What is clear is that many of the attacks in the country claimed by the Islamic State have targeted the minority Shiites, and often in brutal ways. Last week, suicide bombers claimed by the Islamic State barged into a Shiite mosque in the western city of Herat as worshipers were standing for evening prayer and killed at least 37 people.

Those who were killed in Mirza Olang were also Shiites, belonging to Afghanistan’s Hazara ethnic group.

“This was a massacre — they executed 24 people at once, then they killed a woman and her husband near the mill, four people inside the mosque,” said Haji Ghulam Mehdawi, commander of local militias in the province.

Mr. Mehdawi said they had been in touch with the Taliban about retrieving the dead. The Taliban, he said, initially agreed for seven people to travel and pick up the bodies but then backtracked, saying seven people were not enough to carry the dead. Another 10 people have been sent in to help.