KABUL — In recent days, two wars have raged over the Afghan city of Ghazni.

One is a shooting war, in which hundreds of Taliban insurgents overran the eastern city of 270,000 early Friday, burned buildings, left at least 120 government troops and civilians dead, and sent families fleeing across fields to safety. Afghan security forces, backed by U.S. airstrikes, struggled to flush insurgent fighters out of urban hiding places.

The other is a propaganda war, marked by Taliban claims to have shut down the nearby highway, killed hundreds of government troops and seized strategic official facilities, even as Afghan and U.S. officials repeatedly asserted that the city was under government control and that the insurgents were being systematically cleared out.

By late Tuesday, the Taliban appeared to have lost the first war, with the smoldering city relatively calm and most insurgent fighters in retreat. A U.S. military spokesman, Lt. Col. Martin O’Donnell, said that “no enemy activity” was reported in the city all day. In an email, he included aerial images of the Ghazni prison and other intact buildings that the Taliban claimed to have captured or destroyed. He also said the attack served no purpose other than to generate sensational headlines, adding that the Taliban remains incapable of holding territory defended by Afghan forces.

But some analysts said the insurgents may have won the second war, scoring a psychological blow that forcefully contradicts revived hopes for truces and peace talks. That achievement, they said, also weakens confidence in the U.S.-backed government of President Ashraf Ghani and could disrupt parliamentary elections in two months.

“The Taliban’s strategy is to exhaust the Afghan and NATO security forces and further undermine the credibility of the Afghan government, rather than to hold territories they cannot hold,” said Davood Moradian, director of the Afghan Institute for Strategic Studies. The insurgents, he said, are not interested in fighting to talk but in “fighting to win.”

The battle for Ghazni marked one of the more significant challenges in recent years, not only for Afghan forces but for the U.S. service members who advise them. O’Donnell said Tuesday that several U.S. units were involved, including Special Operations forces, the Army’s new security force advisory brigade and Task Force Southeast, a unit of conventional U.S. Army advisers.

U.S. officials would not provide specifics about American troops’ involvement in the fight, but O’Donnell acknowledged that a photograph circulating on social media shows a U.S. Special Operations member who was wounded in the past few days in or near Ghazni. It depicts the service member in a hospital gown receiving a Purple Heart, which recognizes injuries suffered in combat.

At the same time that Taliban insurgents were invading the Ghazni provincial capital with a large ground force, they launched smaller attacks in scattered rural areas this past week, killing 17 troops at a base in the western province of Faryab and 12 policemen in a river town in the northern province of Takhar. Both attacks were typical of recent insurgent tactics that have led to steady inroads in rural regions. As of early this year, the Taliban controlled or influenced 43 percent of Afghan territory, according to an independent U.S. government oversight office. 

But the ferocious assault on Ghazni, which left much of the city without water or power, numerous buildings on fire and hospitals overwhelmed with emergency cases, was the most significant Taliban attack on a major urban center in nearly three years.

In October 2015, insurgents overran the northern city of Kunduz and held it for nearly two weeks amid fierce fighting and U.S. air attacks, one of which mistakenly killed 42 people at a hospital. Elite U.S. troops eventually helped drive the insurgents out of Kunduz in heavy ground combat, according to declassified statements from Green Beret soldiers.

“Tens upon tens upon tens of thousands of rounds were fired against us in our effort to retake the city,” one soldier told investigators after the battle.

“How no [American] was killed, or even wounded, is an absolute miracle,” the soldier said.

Although the Taliban has long been active in Ghazni province, the attack on the city came as a shock, in part because it used massive force, reportedly included non-Afghan Islamist fighters, and showed signs of long-term planning — all at a time when other recent events had raised hopes for a breakthrough in the stalemated conflict. 

During an unprecedented three-day cease-fire in June, Afghan civilians, troops and Taliban fighters celebrated together, leading to talk of a second truce later this month. In July, secret talks were held between Taliban officials and U.S. diplomats in Qatar, and some insurgent officials said they expected follow-up meetings to take place.

Now the intentions of Taliban leaders, some of whom were said to have been taken aback by their fighters’ sudden enthusiasm for mingling with civilians, seem much less clear. Several Afghan experts said the leaders appear to have little interest in negotiating a settlement if it requires them to abandon their goal of imposing full Islamic law, or sharia, after 17 years of fighting. 

The battle for Ghazni illustrates a disconnect between “the Taliban’s military-centric strategy” and the “self-delusion” of Kabul and Washington about prospects for a negotiated settlement, said Moradian, of the Afghan Institute for Strategic Studies. He noted that the Afghan defense forces have shown new “resilience and coherence” and that the Afghan public lives “in dread of the Taliban’s return to power.” But he also said the insurgents’ rigid ideological goal makes them difficult to appease.

Other Afghans, including some officials in Ghazni, complained that their warnings about the insurgent threat were ignored by officials in Kabul. After the assault started, they said, military reinforcements took several days to arrive. And in some cases, local security forces made deals with Taliban fighters — even though the insurgents were far outnumbered — to let them occupy certain parts of the city, these critics said. 

“If the government had heeded those concerns and sent additional forces sooner, this catastrophe would not have happened,” said Atiqullah Amarkhail, a retired army general and analyst. “In some areas, there was no resistance at all. People know the government is weak and divided and corrupt, and they have no confidence in it.”

From several accounts of Ghazni residents who escaped the besieged city, it was clear that the insurgents had sown terror among the civilian populace. It also appeared that the tide of battle turned only after the ­arrival of Afghan special operations forces and U.S. military advisers during the weekend, as well as repeated aerial attacks and flyovers by U.S. military aircraft.

Mohammed Salim, 32, a government worker in Ghazni who fled to another location, said in a cellphone interview Tuesday that he was violently awakened by explosions and gunfire when the attack began early Friday. Taliban fighters later entered his neighborhood and took up sniper positions on nearby rooftops, he said.

“As long as the planes were in the sky, things were calm, but as soon as they left, the Taliban began attacks again,” Salim said. He said he feared for his life, noting that fighters had entered his cousin’s house and found some government ID cards.

“They smashed dishes, windows, doors, the refrigerator and television,” he said. “They would have burned down the house, too, but the neighbors begged them not to do it.”

Dan Lamothe in Washington and Sayed Salahuddin and Sharif Hassan in Kabul contributed to this report.