More than 70 others were wounded in the assault on the Sardar Daud Khan hospital, with explosions and gunfire rattling Kabul's diplomatic district as dense clouds of smoke rose in the sky.
Medical staff hunkered down in the hospital wards posted desperate messages for help on social media, with television footage showing some of them trapped on the ledge of a top-floor window.
"Attackers are inside the hospital. Pray for us," a hospital staff member wrote on Facebook.
Hospital administrators told AFP three gunmen wearing white laboratory coats began spraying bullets after a suicide bomber on foot blew himself up at the backdoor entrance, sparking chaos inside the 400-bed facility.
"I saw one of the attackers, armed with an AK-47 and dressed as a doctor, shooting at patients and guards on the third floor," hospital nurse Abdul Qadeer told AFP.
"They shot my friend but I managed to flee... I had to jump over the barbed wire to escape."
At least two other loud explosions -- including what the defence ministry called a car bomb in the hospital's parking lot -- were heard as Afghan special forces launched a clearance operation that lasted around six hours.
The attackers were gunned down after special forces landed on the roof of the hospital in a military helicopter.
"The attack in the capital's central Wazir Akbar Khan neighbourhood has killed 38 people and wounded more than 70," the Italian-run Emergency hospital said in a statement, adding that a seven-year-old boy was among those seriously injured.
The raid highlights how Afghanistan's warring parties, including government forces, have repeatedly targeted medical facilities, decimating the country's fragile health system and preventing conflict-displaced civilians from accessing life-saving care.
"This egregious and morally reprehensible attack targeted people at their most vulnerable, while they were receiving treatment in the hospital, and also targeted the medical staff caring for them," said the UN Assistance Mission in Afghanistan.
"This cowardly attack reflects a fundamental rejection of the most basic principles of humanity. Without question, it amounts to an atrocity, and the perpetrators must be held accountable."
Islamic State jihadists claimed the attack via a verified Telegram account.
The more powerful Taliban said they were not behind the raid. The militant group, Afghanistan's largest, is known to distance itself from attacks on medical facilities or those that result in high civilian casualties.
The assault comes just a week after 16 people were killed in simultaneous Taliban suicide assaults on two security compounds in Kabul.
Dozens of others were wounded as a suicide car bomber struck an Afghan police precinct in western Kabul and a five-hour gun battle ensued after another attacker sneaked in.
In the second attack last week, a suicide bomber blew himself up at the gates of an Afghan intelligence agency branch in eastern Kabul.
The carnage underscores rising insecurity. The country is bracing for an intense fighting season in the spring as the government's repeated bids to launch peace negotiations with the Taliban have failed.
Afghan forces, already beset by record casualties, desertions and non-existent "ghost soldiers" on the payroll, have been struggling to beat back insurgents since US-led NATO troops ended their combat mission in December 2014.
Kabul last month endorsed US General John Nicholson's call for thousands of additional coalition troops to hold off the militants before the spring offensive.
Extra troops were needed to end the stalemate in the war, Nicholson, the top US commander in Afghanistan, told the US Congress in what could be President Donald Trump's first major test of military strategy.
(This story has not been edited by NDTV staff and is auto-generated from a syndicated feed.)