Media playback is unsupported on your device Media caption 'The walls are pocked with gunshot fire' - Justin Rowlatt visits the site of the attack in Mazar-e Sharif

Afghanistan's defence minister and army chief of staff have resigned in the wake of Friday's Taliban attack that left scores of soldiers dead, the presidential palace says.

The attack happened at an army base near the northern Mazar-e Sharif city.

Insurgents targeted troops leaving Friday prayers at the base's mosque and in a canteen, the army said.

It was the Taliban's deadliest attack on the armed forces since US-led forces drove them from power in 2001.

The resignations coincided with the arrival of US Defence Secretary Jim Mattis for a visit to Kabul.

Mr Mattis said the attack "shows why we stand with the people of this country against such heinous acts".

Insurgents are also reported to have attacked a base in eastern Khost province. A car bomb detonated at the entrance to Camp Chapman, a US-operated base, and there were a number of Afghan casualties, Reuters reported.

How did the Mazar-e Sharif attack unfold?

About 10 Taliban insurgents dressed in Afghan military uniforms and driving military vehicles made their way into the base and opened fire.

Many of those who died were young recruits. Witnesses described chaotic scenes as the soldiers struggled to work out who was friend or foe.

The attackers were armed with guns, grenades and some were wearing suicide vests, reports said. The defence ministry said the attackers were all killed.

The Afghan defence ministry has not released firm casualty figures, only saying more than 100 people were killed or injured.

Other officials have told BBC that at least 136 people died.

But some sources say the toll was even higher. One eyewitness told the BBC he counted 165 bodies.

Image copyright Wesley Abbott Image caption Insurgents targeted Afghan troops leaving Friday prayers at the base's mosque

Image copyright Wesley Abbott Image caption Taliban fighters also attacked the base's canteen

Image copyright Wesley Abbott Image caption They used pick-up trucks to enter the compound

Analysis by Waheed Massoud, BBC Afghan Service editor, Kabul

The resignations of Defence Minister Abdullah Habibi and army chief Qadam Shah Shahim might reflect well on the Afghan government from an ethical standpoint - but they will do little to prevent future similar attacks.

Under former President Hamid Karzai, the then spy chief Amruallah Saleh and Interior Minister Atmar resigned over failing to prevent mortar attacks at a traditional grand assembly. However, suicide attacks and other insurgent operations only intensified after that.

A stronger international crackdown on covert support channels to insurgents in Afghanistan, a stronger Afghan intelligence force, and a more professional, corruption-free and better trained army might help.

Image copyright Reuters Image caption Afghan National Army soldiers have now sealed off the base

The Afghan commando forces are a positive example.

It is not just the top tier of the military leadership that needs to change; what would have an impact would be injecting a sense of responsibility across the force.

Why have officials resigned?

The resignations were announced in a brief statement.

No explanation was given but the attack has caused widespread anger, with many questioning the government's ability to counter the Taliban insurgency.

It comes just weeks after the deadly assault on the military hospital in Kabul.

That attack was blamed on the so-called Islamic State, but many have questioned the official narrative, saying the attackers shouted pro-Taliban slogans.

People have also questioned the inability of the authorities to prevent such attacks, the lack of clarity regarding death tolls and the possibility of insider involvement.

The recent fall of Sangin in the south - a strategically important centre - has also shaken confidence in the defence establishment.

So the security situation is deteriorating?

Since the US-led Nato troops ended their mission, the Afghan military has struggled to contain the insurgents.

According to a US government estimate in November 2016, the government had uncontested control of only 57% of the country - down from 72% a year earlier.

IS militants have also established a small stronghold in the east and have carried out attacks in Kabul.

Earlier this month the US dropped its largest ever conventional bomb on suspected IS fighters, killing dozens.

But Mirwais Yasini, an Afghan MP, said the US focus on IS was misguided, when the Taliban was the biggest threat.

Media playback is unsupported on your device Media caption The moment MOAB bomb struck IS cave and tunnel systems

What is the US doing?

There are still about 8,400 US troops and 5,000 Nato soldiers in Afghanistan helping to build local forces.

In February the top US commander in the country, General John Nicholson, said several thousand more were needed.

But White House policy remains unclear. Donald Trump's administration has not yet appointed an ambassador to Afghanistan or set out its strategy for the region.

Recent visits, however, could signal new engagement.

Earlier this month, National Security Adviser HR McMaster was in Kabul and said officials would present Mr Trump with a "range of options".

The surprise arrival of Mr Mattis could suggest new focus from the White House on this long-running conflict.