Taliban insurgents fought their way into a major city in northern Afghanistan on Monday, driving back stunned security forces in a multi-pronged attack that also sent Afghan officials and U.N. personnel fleeing for safety.

The fall of Kunduz would be a huge blow to the Western-backed government in Kabul and would give Taliban insurgents a critical base of operations beyond their traditional strongholds in Afghanistan’s south. Afghan government leaders and the U.S.-led coalition here view the battle for Kunduz as a key test of the Afghan security forces in their continuing fight with the Taliban.

For the moment, Afghan officials acknowledged, much of the city is in Taliban hands, and Afghan authorities were left struggling over how to turn the tide, although they insisted that they would prevail once they mount a counterattack.

The assault began shortly before dawn when hundreds of Taliban fighters advanced into the city from four directions. Although Afghan security units were backed by helicopter gunships, the Taliban took over a 200-bed hospital and overran the local prison, freeing hundreds of prisoners. From there, they seized the office of the governor, who was not in the city at the time.

The militant group posted triumphant pictures to Twitter showing Taliban fighters hoisting their white-and-black flag throughout the city.

Kunduz, a hub for the country’s once relatively stable grain region about 150 miles north of Kabul, would hand the Taliban one of the linchpins of Afghanistan’s economy. It was the last Taliban stronghold in northern Afghanistan in November 2001, when the group’s grip on the country collapsed in the face of opposition fighters and U.S. airstrikes.

If Taliban fighters succeed in keeping control of Kunduz, it would be the first time in 14 years that they have seized and held a city.

On a broader level, the attack displays the Taliban’s battlefield power and coordination even as the radical Islamist insurgency faces internal discord following the acknowledgment in the summer of the death of its longtime leader, Mohammad Omar.

The U.S. military still has 9,800 troops in Afghanistan, but it was unclear Monday whether any American personnel were stationed near the fighting in Kunduz.

Army Col. Brian Tribus, a military spokesman, said that the American-organized coalition has not conducted any recent airstrikes in Kunduz but that it was providing intelligence and surveillance support to the Afghan army. Coalition forces “train, advise and assist” the Afghan military, but Tribus declined to discuss specifics of the mission, citing concerns about operational security.

Afghan security officials said that government forces withdrew Monday in an attempt to avoid civilian casualties and that they are planning a counteroffensive to regain Kunduz — a city that has already been the target of Taliban attacks twice this year.

“We are prepared, and measures have been taken to recapture the city,” the deputy interior minister, Ayoub Salangi, told reporters.

In Washington, a U.S. defense official, speaking on the condition of anonymity, said officials at the Pentagon believe that, based on previous Taliban assaults on population centers, Afghan ­forces will probably be able to prevail.

The United States can conduct airstrikes only if Afghan forces are judged to be “in extremis,” or facing a critical threat from militant forces, the defense official said, adding: “I wouldn’t rule out there being some sort of extremis situation.”

Taliban fighters have taken all the major government buildings in Kunduz, including the police and intelligence headquarters, and set fire to some of them, said Amruddin Wali, a member of the provincial council.

“This will have a lot of impact on morale on all sides,” said Atiqullah Amarkhail, a retired Afghan general and military analyst. “Government forces may lose morale, while opposition forces’ morale will be boosted as they can now say they can capture cities.”

But he noted that Taliban gains do not necessarily foreshadow “the fall of the entire north or the fall of the government.”

Over the summer, the Taliban was able to steadily expand its reach across the country. Most major population centers, including Kabul, remain firmly under the control of government forces but still vulnerable to terrorist attacks. Across large swaths of rural Afghanistan, however, the Taliban has also been seizing strategic targets that form the backbone of the Afghan economy.

Hafizullah Benish, the agriculture director for Badghis province, said in an interview over this past weekend that the Taliban now controls much of Afghanistan’s $30­ million pistachio crop in the northwestern part of the country.

Taliban gains in Helmand province in the south forced the evacuation of British engineers from a hydropower project this month, the Guardian newspaper reported.

Dominic Medley, spokesman for the United Nations Assistance Mission in Afghanistan, said Monday that all U.N. staffers were evacuated from the Kunduz area as security deteriorated.

The Taliban fighters were outside Kunduz all summer. In June, the Taliban briefly gained control of two of the city’s six districts. Within days, however, Afghan security officials had driven them out again.

Monday’s attack may have been timed to coincide with the first anniversary of Afghanistan’s new national unity government.

On Sept. 29, 2014, after a months-long stalemate over election results, Ashraf Ghani was sworn in to replace former president Hamid Karzai. The second-place finisher in that election, Abdullah Abdullah, was named to a new position of chief executive officer.

Ghani and Abdullah have struggled to oversee an Afghan military that appeared surprised by the ferocity of Taliban attacks this summer.

This year’s fighting season was marked by clashes not only in historical Taliban strongholds in the southern part of the country but also in northern areas that had previously been relatively secure.

The insurgency has been joined by thousands of fighters who have been driven from neighboring Pakistan because of the ongoing Pakistani military operation in that country’s tribal belt.

But in the summer, Ghani’s government and Army Gen. John F. Campbell, commander of the U.S.-led coalition, stressed that Afghan forces were well prepared to prevent significant Taliban gains on population centers.

Faisal Sami, an Afghan senator from Kunduz, said he and other local officials had grown increasingly worried in recent months that Ghani’s government did not have a serious plan for keeping the city safe.

“This is a major embarrassment to this government,” Sami said.

In recent weeks, there were also growing calls for Ghani to replace the governor of Kunduz province, Omar Safi, who was away on Monday.

“The main reason for the deterioration of the security situation and the Taliban’s gains is bad management of the affairs by the governor and lack of attention from the central government,” said Mohammad Yousuf Ayoubi, the chief of Kunduz’s provincial council.

Mohammad Sharif in Kabul contributed to this report.

Read more:

Afghans who once watched war from afar forced to flee as front lines shift

Taliban has a major northern Afghan city within its grasp for the first time since 2001

Today’s coverage from Post correspondents around the world