Doctors at the Kunduz hospital were meant to be catching up on their sleep in an underground bunker, but after five days of raging battles the wards were jammed full and Dr Satar wanted to stay near his patients.

“You are so chicken-hearted,” he teased Hamidullah*, a young registrar who was also desperately in need of rest. “Stay here,” he told him. For Satar, his decision to stay would be his own death sentence.

Two hours later, in the early hours of 3 October, an American gunship attacked the Médecins Sans Frontières hospital, killing at least 22 people and injuring dozens more, in one of the worst civilian-casualty incidents of the war in Afghanistan.

The first shells to hit the building landed a few metres from where some doctors were napping, at the centre of three hallways leading to the operating theatre, the intensive care unit and the emergency ward.

Shrapnel peppered Hamidullah in the back and buttocks, as he screamed out verses from the Qur’an. His colleague and friend Satar was just a few steps behind, but close enough to take far more of the force of the blast. When Hamidullah turned round to check on him, one of Satar’s arms was missing, ripped off by the explosion.

“He was not able to stand up, not able to do anything. He said: ‘I’m dying, please don’t leave me alone,’” Hamidullah recalled. As pieces of the collapsing roof showered the two men and smoke filled the air, Satar collapsed to the floor. Bleeding and coughing, Hamidullah fled in search of an exit.

Like most workers in MSF hospitals, the doctors had considered themselves relatively safe until the moment the first rounds hit. Medical facilities are protected under international humanitarian law, and the charity they work for has won the Nobel peace prize for its work treating the sick and injured in some of the world’s most difficult and dangerous conflict zones.

It has a reputation for meticulous attention to safety and maintaining links to all groups fighting in areas where it operates, to ensure they respect its legal neutrality.

The charity has now pulled all its staff from Kunduz and demanded an independent inquiry into how American forces came to spend more than an hour shelling one of the few places of shelter left in the battered city.

“We need to know what happened and why it led to an airstrike on a hospital which has been known in the region for the last four years, which has been treating thousands of people,” the MSF president, Dr Joanne Liu, said in London this week. “Until we understand how it happened, we cannot go back to Kunduz.”

MSG staff explore the hospital building destroyed in a US airstrike in Kunduz. Photograph: Xinhua/Landov/Barcroft Media

There were 80 staff and 105 patients in the hospital buildings at the start of the attack, which lasted from 2.08am to 3.15am. So far, 22 people have been confirmed dead, including 12 staff and three children, and more than three dozen others injured. More than 30 staff and patients are still missing.

Afghan officials originally said the attack was prompted by insurgent fighters who took cover in the compound and used it to launch attacks. As recently as 9 October, the acting defence minister, Masoom Stanekzai, told CNN that the Taliban had been fighting from the walls of the compound, as the battle to wrest control of the city back from the militants continued.

However, that does not match the stories of witnesses such as Wahidurehman, who has lived across the street from the now ruined compound since long before it was a hospital. Originally a textile factory, it was converted to a medical centre four years ago.

“The rumours about Taliban being inside the hospital are all lies,” he told the Guardian by phone. “During the week I saw the hospital guards disarm everyone who came inside [to admit patients], including Taliban.”

Shaken awake by the first large blast, Wahidurehman said, he was able to look into the hospital compound where he saw screaming doctors and patients, some dead and some alive. A fire raged inside the main building, shooting tongues of flames towards the sky. The walls of the building, though blackened, were still standing and the remaining buildings were largely undamaged.

“I don’t know what kind of laser weapon this was that only created fire and didn’t destroy the buildings,” he said, then he broke down and wept. “Even if we wait 20 years, I don’t think Kunduz will get such a hospital again. The foreigners won’t come back if they are not safe. Each doctor is worth one billion dollars. We can never again find such brave doctors willing to work on the frontline.”

The team had treated almost 400 people in the week that Kunduz fell under Taliban control, as government troops battled to take it back. But late that Friday evening, the flow of patients was trailing off, allowing several doctors to seize their chance to catch up on much-needed sleep.

Fazal, 27, a member of the hospital’s administrative staff, had headed to the underground shelter with his mind racing, but dropped off almost immediately. He was woken by the metallic screech of incoming ammunition, and then the deafening roar of an explosion. He choked on dust and smoke.

The destruction appears to have been caused by a US AC-130 gunship aircraft circling in the darkness, loaded with 25mm and 40mm cannon, as well as a 105mm howitzer, the Washington Post reported.

On each pass, five times approximately every 15 minutes, the plane fired and hit the main hospital building “repeatedly and precisely”, MSF stated, indicating that the facility was targeted on purpose.

A charred room in the destroyed MSF hospital in Kunduz. Photograph: Najim Rahim/AP

Liu said MSF’s ability to work in some of the world’s most difficult conflict zones had been put at stake by the attack. “[Parties to a conflict] cannot target patients, medical facilities, ambulances, healthcare workers,” she said. “That’s the bottom line, that’s what the Geneva convention gives us, and we have been working on the understanding that people respect that. If somehow we decide this is somehow not respected any more, then we question everything after that.”

American forces should have known the hospital was protected. Four days prior to the attack, the charity reminded all parties, including the US military, of the exact GPS coordinates of the hospital.

And as the shells and gunfire rained down, international MSF staff in Kunduz called US officials in Kabul and Washington “through agreed channels that they had proposed to us,” Christopher Stokes, MSF’s general director, told the Guardian. “We don’t even know if our call stopped it. They may have stopped because there was nothing left to bomb.”

The US military initially conceded only that their airstrike might have caused “collateral damage” to the hospital. The top commander in Afghanistan, John F Campbell, later admitted – in the fourth attempt at explaining the devastating attack – that the hospital was “mistakenly struck”.

Since then, a former intelligence official has told the Associated Press that American special operations forces who planned the attack knew it was a hospital because they had been gathering intelligence on it for days. They believed it was being used to coordinate Taliban activity, and considered the strike justified. The report said it was not clear whether the commanders in the gunship firing on the site knew it was a hospital or the details of the allegations.

On Monday the Pentagon acknowledged that US and Afghan troops had driven a military vehicle through a locked gate to the hospital on 15 October, forcing entry to the site. Captain Jeff Davis said the troops wanted to inspect damage, and had not known that MSF staff were there.

Human rights organisations point out that intentionally targeting a medical facility is a crime. For the survivors, and for the injured victims of fighting still going on in Kunduz, it is a tragedy.

As Hamidullah made his way through the corridor, searching for the exit, he saw another injured colleague, Dr Amin, calling for help. “He was on the floor, asking his family for forgiveness because he wasn’t going to live,” he said.

Together they made it outside where Amin leaned against the wall. That was when Hamidullah noticed that his colleague had lost a leg. He carried Amin to a nearby tree where they both collapsed. Hamidullah pressed his back and buttocks against the ground to stop the bleeding until a group of co-workers came and carried Amin away.

From a makeshift operating table, Amin called his wife and three children. He died two hours later of blood loss, according to witnesses.

From the shelter, Fazal heard the attack come to a close and went outside. He saw the main building in flames. In the intensive care unit, six patients who had been unable to escape burned in their beds. He saw the charred bodies of colleagues and patients.

A security guard sat slumped by the gate, having lost his fingers and one leg, and bleeding from the head. “We kept him alive until 5am,” Fazal said. “My entire body was full of my colleagues’ blood; my hands, my head, my clothes. When I walked around to try and help one colleague, I found another body. It was horrible.”

Some of the worst injured patients and staff were driven to neighbouring provinces or even to the Italian-run Emergency hospital in Kabul, around 180 miles south.

A boy who survived the MSF airstrike receives treatment at the Italian aid group Emergency’s hospital in Kabul. Photograph: Wakil Kohsar/AFP/Getty Images

Luca Radaelli, director of Emergency, said the patients he admitted bore marks of double trauma after first being injured, and then bombed in a place they assumed to be safe. “I don’t think there is anything worse than that,” he said.

One of those patients, 42-year-old Lotfullah, had been severely injured when a piece of shrapnel slashed his leg. Before the attack, he had been waiting to be operated on, but he was mobile enough to escape the bombing along with about 50 other patients who then sought refuge in two containers outside the compound, he said.

At dawn they hired taxis to take them to the nearest city with a functioning hospital, but Lotfullah’s wound had already begun to turn black, and when he reached doctors hours later they had to amputate. “If my turn had come up at MSF, I would not have lost my leg,” he said.

Fazal has not been able to rest since those few minutes before the attack when he allowed himself to drift off to sleep in the hospital basement. The sound of a plane startles him, and his eyelid is twitching. “That night keeps coming into my mind. I can’t relax,” he said. “But the doctor has told me I’m safe now.”

Additional reporting by Mokhtar Amiri

* Some names have been changed because MSF staff are not authorised to speak with the press.