KABUL, Afghanistan — The nerve center of Afghan and NATO combat activities in northern Afghanistan is a sprawling military base in Balkh province. There, thousands of Afghan National Army troops live and train, regional deployments and attacks are planned, and U.S.-supplied helicopters and fighter planes are launched to support Afghan troops battling the Taliban.

On Friday, in a stunning blow to the Western-backed war effort, the base on a sun-baked plain near the city of Mazar-e Sharif became the target of the deadliest single attack by Taliban insurgents since their regime in Kabul was overthrown in 2001. A spokesman for the Islamist militia claimed responsibility for the assault.

There were conflicting reports on the number of casualties, but some Afghan officials said Saturday that at least 140 people had been killed and 60 injured when a handful of Taliban fighters, disguised as Afghan military personnel, entered the base in army vehicles and opened fire. A Defense Ministry spokesman said that only 100 were confirmed dead, however.

Witnesses and survivors said the assailants sprayed gunfire among mostly unarmed Afghan soldiers and officers, many of whom were either just leaving weekly prayers at the base’s mosque or eating in its canteen.

There were no reports of NATO service members being killed on the base, where a group of German military advisers has been stationed to train and advise Afghan forces.

Gen. John Nicholson, the top U.S. commander in Afghanistan, praised Afghan commandos for bringing an “atrocity to an end.” Afghan President Ashraf Ghani flew to the area Saturday to console victims’ families.

The attack lasted for nearly six hours, with the insurgents firing guns, throwing grenades and detonating suicide bombs, officials and witnesses said. A Taliban statement said four of the attackers were army defectors who had served at the base, and it called the rampage a “prelude” to the militants’ traditional spring offensive.

But the Taliban has hardly let up its campaign this past winter, instead repeatedly attacking strategic cities and towns in scattered regions, and gradually gaining influence or control over greater portions of the country.

One of the insurgents’ major targets has been Kunduz city in the north, which borders the country of Tajikistan. Afghan troops and war pilots have been repeatedly deployed there from the assaulted base.

The attack came less than a week after forces loyal to the area’s powerful regional governor reportedly drove Taliban fighters from much of the province, long considered one of the most secure in Afghanistan. It also came five months after a suicide bomber managed to enter the main U.S. military base at Bagram, near Kabul, killing four people.

The insurgents reportedly managed to get past two security gates by pretending to bring wounded soldiers for medical treatment, a tactic also used last month by Islamic State attackers who invaded a military hospital in Kabul and slaughtered scores of people inside.

In Friday’s attack, officials said, two of the assailants blew themselves up and seven were killed.

The full extent of the carnage was not known until Saturday, after authorities finished searching the base for survivors. All day, the reported toll of dead and wounded kept rising. A provincial political leader, reached by phone, said that at one point during the battle, the base had run out of coffins.

Ghani, during his visit to the area, described the attackers as “infidels,” because they had fired on soldiers praying inside the mosque. Taliban officials denied the charge, but photos circulating on social media showed a bullet-riddled pulpit.

“When I came out of the mosque, three people with army uniforms and an army vehicle started shooting at us,” an injured soldier named Mohammed Hussain told an Afghan news outlet. He said one of them had set a machine gun in the window and “shot everyone in his way.”

Relatives of some dead soldiers, waiting outside the base to receive the coffins, complained of poor security measures on the base and speculated that the attackers must have had inside help.

The Afghan defense forces, largely responsible for waging the war after the withdrawal of most NATO troops in 2014, have suffered from desertions, corruption and defections to the other side.

There are currently about 8,400 U.S. troops and another 5,000 NATO forces in the country, and Nicholson and other U.S. military officials have said several thousand more are needed to continue training and supporting Afghan forces.