WAZIR TANGI, Afghanistan — Deep inside Taliban territory, high in the mountains that line the Afghanistan-Pakistan border, a top-ranking militant commander cradling a Kalashnikov boasted of the group's victory against the Islamic State here. He declared that "when the Taliban comes, the peace will also come."

But a deadly Taliban attack on the U.S. military base in Bagram just hours earlier undermined his message of comity. Even as the group dispatched negotiators to forge a peace deal with the United States, commanders and fighters were describing a militancy committed to the use of violence to achieve its goal of regaining political power after more than 18 years at war with U.S. and Afghan forces.

The extraordinary briefing earlier this month by the acting director of the Taliban’s military operations, Moulawi Muhammad Ali Jan Ahmed, for a small group of Western journalists signaled the militants’ quest for legitimacy on the global stage after years of being seen as enemy combatants. The Taliban controls or contests roughly half of Afghanistan, and peace talks could formalize the group’s power.

“For the last 18 years, we have fought the Afghan government and the Americans, and our struggle will continue,” said Ahmed, dressed in a black turban and camouflage jacket and flanked by deputies and aides. “They occupied our land. Our attacks will continue and also increase. Any power we have we will use against them, God willing.”

The Dec. 11 Bagram attack, which killed two and wounded more than 70, derailed the most recent round of peace talks in Doha, Qatar. Still, Taliban commanders cited it as an example of the increased pressure the group plans to place on American and Afghan government forces in the coming months.

“Compared to 10 years ago, yes, I believe the world sees us as more legitimate, and all of that is through our violence,” said Saied, who is one of 14 Taliban military commanders responsible for Nangahar province and who, like many Afghans, goes by a single name.

Mullah Nik Muhammad Rahbar, 28, a Taliban commander responsible for Kabul province, pointed to the resources freed up by the conclusion of the fight against the Islamic State in Nangahar, saying the Taliban would be able to shift back to conducting more high-profile attacks in Kabul and elsewhere.

“Thank God you saw what we achieved against Bagram today,” he said. “We launch attacks in Kabul because there are many foreigners there, many targets for us.”

Despite Taliban claims that it targets only foreign forces and Afghan government facilities, attacks by the group have killed 922 civilians and wounded 2,901 just this year, according to the latest U.N. report. Taliban commanders in Nangahar said the group was doing everything it could to reduce civilian casualties, including setting up a commission to investigate attacks that kill civilians and imprison those responsible.

Before moving deep into the mountains to meet senior leadership, Taliban fighters escorted journalists through the Sherzad district’s Zawa village. Forming a convoy of beat-up Toyota Corollas blasting melodic Koran readings and pickup trucks flying Taliban flags, the men pointed out homes they said were crippled by airstrikes, a school they said was burned out in a night raid and a clinic they said was raided of its medical supplies.

The United States dedicated hundreds of troops to the Islamic State fight here alongside thousands of Afghan government forces, who conducted clearing operations “valley to valley, house to house,” said a U.S. official, speaking on the condition of anonymity to discuss the subject with the media. And a massive, years-long American air campaign killed hundreds of mid- and high-level Islamic State commanders in Nangahar.

A group of about a dozen farmers, grazing sheep near the remnants of a house hit by an airstrike, revealed the intensity of the fight from the air against the Taliban and Islamic State. They all said they had lost a family member to an airstrike or drone attack.

A 35-year-old shepherd named Rahimullah said he lost his uncle to a drone strike and could not retrieve his body from a nearby hilltop for hours as the drone continued to circle above. An elderly man, Seghagan, told how his son was killed in a garden by an airstrike two years ago. Ameen, 28, said he lost three small children when an airstrike hit his house seven months ago.

But it was not American or Afghan military efforts that booted the Islamic State from this area, Taliban commanders said. The Islamic State posed an existential threat to the Taliban by recruiting fighters disaffected by their leaders’ decision to engage in peace talks, so commanders called in a surge of Taliban forces from southern Afghanistan to aid in the push.

“We want the world to know us for more than just killing,” said Saied, the Taliban commander. “We are also fighting terrorists.”

American and Afghan government officials concede the Taliban’s contributions to the fight against the Islamic State were significant.

The Taliban “were catastrophically successful” in the fight against the Islamic State, the U.S. official said.

Despite the U.S. and Afghan ground and air operations, “the Islamic State was able to recede deeper into those mountains and just wait until we leave and then just reassume that territory,” the official said. “Unlike us, the Taliban didn’t need to return to bases and they didn’t need to return to official positions in Jalalabad or other places.”

That the Taliban invited journalists to its territory in Nangahar to flaunt the group’s victory over the Islamic State reflects “a slow realization within the Taliban that they have a terrible international image and, more precisely, that that’s a problem,” said Ashley Jackson, a researcher with the Overseas Development Institute who has done extensive fieldwork studying the Taliban.

“I think this is part of their preparation for what they see as peace talks which are imminently about to give them a huge amount of control over Afghanistan,” she added.

A peace deal between American and Taliban negotiators is expected to lead to inter-Afghan talks, with the aim of forming a unified government in which some Taliban officials would assume formal positions of power. But any such accord could be years away — U.S.-Taliban talks have repeatedly broken down, and the Afghan government remains deeply divided on the issue of peace with the Taliban.

The possibility of peace also raises the question of what happens next to the estimated 80,000 fighters in the Taliban’s ranks.

The mid-level and senior Taliban commanders gathered in Nangahar said that if a peace deal were successful, they would expect to “continue to serve” their country.

“I would want to serve my country the way I serve now,” said Ahmed, who oversees operations in 20 provinces. “Or perhaps even more.”

But the dozens of rank-and-file Taliban members on the sidelines of the briefings and media tours said they expected to lay down arms and return to civilian life. But American officials have warned that Afghanistan is ill prepared for ex-combatants to be reintegrated into society because the government lacks the ability to track them and ensure their safety.

Ali Kheil Umari, 35, who was a math teacher at a school funded by UNICEF before he lost a family member to an airstrike and joined the Taliban, said he would return to teaching. But for most of the other men, being part of the Taliban is the first and only profession they have had as adults.

Abdul Rahman, 20, who joined the Taliban at the age of 14, self-consciously hid one arm under his jacket. He had lost a hand during his time as a fighter trying to defuse an explosive. In the event of peace, he said, he would happily return to farming in Nangahar.

But he acknowledged the transition would be difficult. He had no experience working a farm. He also betrayed a reluctance to leave behind what he sees as a religious calling.

“With farming, you are feeding yourself in this life,” Rahman said. “With jihad, you are feeding yourself in the afterlife.”

Aziz Tassal in Jalalabad contributed to this report.

Correction: An earlier version of this story misspelled the name of the area where much of the reporting took place. It is Wazir Tangi.