BALKH, Afghanistan — The Afghan defense minister and the army chief of staff both resigned on Monday as the government faced mounting pressure over a Taliban sneak attack that killed more than 160 soldiers last week.

The resignations, followed by broad reshuffling of the army’s regional leadership, came as the American secretary of defense, the former Marine general and Afghanistan veteran Jim Mattis, arrived in Kabul for his first official visit in his new post. The Trump administration is reviewing its Afghanistan policy and considering a request from the American military commander in the country for thousands of additional American troops to help the Afghan forces hold the line against a resurgent Taliban.

The Afghan defense minister, Gen. Abdullah Habibi, and the chief of staff, Gen. Qadam Shah Shahim, said in a joint news conference that their decision to step aside was because of widespread anger at the attack against the 209th Army Corps in northern Balkh Province — the single deadliest Taliban assault of the long war against Afghan forces.

In that assault, on Friday, 10 attackers drove onto the country’s largest base in the north disguised as soldiers carrying wounded comrades. Some of them detonated explosives among hundreds of unarmed troops who were emerging from Friday Prayer, while others went on a rampage, gunning down soldiers, most of them new recruits in training.

American military officials said on Monday that they had intelligence that showed the Haqqani network, a ruthless wing of the Taliban based in Pakistan that has become an integral part of the insurgency’s leadership, was behind the attack. One senior United States military official, speaking on the condition of anonymity to discuss intelligence, said the attack had been planned over four to six months and was too sophisticated and “calculated” to have been conducted by other branches of the Taliban.

The resignations of President Ashraf Ghani’s defense secretary and army chief came as no surprise, American officials said, because they came on the heels of another devastating attack in March. In that assault, militants entered the Afghan Army’s main hospital in Kabul and killed more than 50 people in a siege that lasted nearly seven hours and was claimed by the Islamic State.

Mr. Ghani, American officials said, had to show that there was some degree of accountability in his government. But Mr. Ghani, in fact, stepped in to protect the defense minister when Parliament was trying to oust him after the hospital attack.

Gen. John W. Nicholson, the commander of the American-led war effort in Afghanistan, said that while the Islamic State claimed responsibility for the hospital attack in March and the Taliban claimed responsibility for the base attack last week, military officials were watching for “convergence” between the two groups in Afghanistan. The style of attacks, General Nicholson said at a news conference with Mr. Mattis, was similar, and he added that it was “quite possible that Haqqani” was responsible for the base attack last week.

The Afghan president’s office said that Mr. Ghani and Mr. Mattis were meeting ahead of the Trump administration’s decision on whether to increase the number of American troops in Afghanistan, which General Nicholson has asked for. Mr. Mattis said he had not yet decided whether he would recommend a troop increase.

American Defense Department officials have said that one option under consideration would be to send 3,000 to 5,000 additional troops to Afghanistan. But the open-ended commitment that some military experts believe is required of the war here would mean that Mr. Trump would be backing away somewhat from his “America First” stance that he took during the presidential campaign.

General Nicholson’s request for more American troops came after the Afghan government again missed much of its winter goal to urgently overhaul the security ranks in order to put up a stronger fight against the Taliban, who overran large parts of the country last year and now threaten several cities.

The aging top layer of commanders and generals of the Afghan forces is widely seen as corrupt. And there are rising concerns that the overreporting of troop and police strength, allowing commanders to line their pockets with pay for nonexistent “ghost” soldiers, has partly obscured a deep problem with high attrition and desertion rates.

At the urging of the American military leadership, Mr. Ghani finally set up a board to monitor senior security appointments and institutionalize promotions based on merit and away from political patronage. But a couple months in, the board seems in disarray.

On Monday, the president’s office confirmed that Mr. Ghani had also replaced the commanders of four army corps, including the 209th. But officials and analysts said the appointments amounted more to an emergency reshuffle than any sign of effective change.

Saleh Mohammad Registani, an Afghan military analyst, said the forces still lacked a proper procedure to track the performance of officers. The senior appointments, he said, are made based on political considerations.

ToloNews, the Afghan television channel, reported that the commander of the 209th Corps, Gen. Mohmand Katawazi, had been listed for retirement months ago, but Mr. Ghani had extended his tenure to lead the biggest army corps in the north.

“You have a young force full of energy but led by a cadre that is old and not experienced in this kind of battle,” Mr. Registani said. “When you speak to soldiers, they say even battalion commanders don’t come to the battle, let alone the brigade or regiment leadership.”

Just an hour after news broke that he had been replaced as the commander of the 209th corps, General Katawazi was seen driving to the airport in Balkh Province in a black armored vehicle with an escort of army ranger trucks.

The hatred of the ranks for the leadership was clear when one of the police officers at the airport checkpoint was told the man in the black armored vehicle was the army corps commander who had just been replaced.

“Replaced?” the police officer shouted. “Ashraf Ghani should hang him from the blades of a moving helicopter!”

A version of this article appears in print on , on Page A10 of the New York edition with the headline: 2 Top Afghanistan Military Officials Resign Amid Anger Over Deadly Taliban Attack. Order Reprints | Today’s Paper | Subscribe