As NATO forces tried to fend off a Taliban attack during the visit of US Secretary of Defense James Mattis to Kabul, a “tragic malfunction” during a US air strike resulted in a number of civilian casualties.

“During a failed attack today, insurgents fired several rounds of high-explosive ammunition, including mortars, into the vicinity of Hamid Karzai International Airport and detonated suicide vests endangering a great number of civilians,” said a statement from NATO’s Resolute Support Mission in Afghanistan.

“The Afghan Crisis Response Unit 222 responded quickly to confront the attackers and end the assault. US forces acting in support conducted an airstrike. Tragically, one of the missiles malfunctioned, causing several casualties.”

Officials said an investigation into the "malfunction" is underway and promised to release more details about the attack. NATO also blamed the Islamist group for operating in areas with a high density of civilians.

“We take every precaution to avoid civilian casualties, even as the enemies of Afghanistan continue to operate in locations that deliberately put civilians at very high risk,” insisted the statement published Wednesday evening.

Earlier, the Afghan interior ministry said the three attackers along with one person had been killed and 11 others wounded during the early morning assault on the airport. It is unclear if any of these casualties were inflicted by the US malfunction.

US military officials told American reporters that up to 40 projectiles hit the Hamid Karzai airport, including 29 rocket-propelled grenades.

The Taliban said it specifically targeted Mattis and NATO Secretary-General Jens Stoltenberg, neither of whom had announced their visits in advance, though both men had already left the area when the rockets began to hit.

Islamic State, which has less influence in Afghanistan, also issued a less credible statement, claiming that its fighters were responsible for killing over 80 “crusaders and apostates.”

Mattis’ visit was the first since US president Donald Trump announced a boost of almost 4,000 soldiers to the existing American military contingent in the country, which would bring the total number of NATO troop in the country to over 17,000.

The US Secretary of Defense, who assumed office in January, said at a news conference Wednesday that the Pentagon would ensure that Afghanistan “doesn’t again become a safe haven for international terrorists.”

In its latest assessment, the Washington think-tank, Foundation of Defense of Democracy, estimates the Taliban either controls, or is fighting for 45 percent of the mountainous state’s territory. Other radical groups, such as Al-Qaeda offshoots, and the local branch of Islamic State, also have deep roots in the country, 16 years after it was invaded by the international coalition in the wake of the 9/11 attacks.

With both the Taliban and Islamic State claiming responsibility for the attack, the two groups could actually be working together, Sabah Al-Mukhtar, President of the Arab Lawyers Association in the UK, told RT.

“They have announced a little while ago that they’re going to conduct joint operations” and this could well be the case, he said. Al-Mukhtar is also skeptical about the new US and NATO strategy of bringing additional troops to Afghanistan.

“It’s unlikely to create anything. They’ve had many more solders there – they didn’t achieve anything. Adding a few more thousand under the pretext that they’re going to train people – this is a gimmick thing” as most of the fighting against Taliban and IS is already done by the Afghanis, he said.

US political analyst, John Raines, told RT the terrorists likely had relevant intelligence from Kabul airport and were “planning this attack for a while.”

“You have to think that, probably, intelligence escaped. It doesn’t necessarily means that they knew that the Secretary of Defense was, actually, going to be there. They just might’ve known that someone really important is coming to the airport,” Raines said.