The ISIS-affiliated news agency Amaq said the terror group claimed responsibility for the attack near Kabul's heavily fortified diplomatic quarter.
First, a suicide bomber blew himself up at the south gate to the Sardar Mohammed Daud Khan hospital, said Sediq Sediqqi, Afghan Interior Ministry spokesman. Known as "the 400-bed hospital," it's the country's biggest and best-equipped medical facility.
Three gunmen then invaded the hospital, made their way to the second and third floors and opened fire, Sediqqi said. Among those killed were Afghan military personnel recovering from battle wounds, doctors and hospital employees.
Security forces and police mounted a counteroffensive. Heavily armed soldiers and armored vehicles surrounded the facility, a helicopter landed on the roof and a few patients climbed out of windows and stood on a ledge to escape the violence, video shows.
Soldiers killed the attackers about 3:30 p.m. local time (6 a.m. Wednesday ET) after six hours of fighting.
As soldiers cleared the building, they discovered bodies and the number of casualties quickly grew.
More than 50 people were wounded and taken to the Wazir Akbar Khan hospital, said Smael Kawosi, media relation officer for the Ministry of Health. It's not known whether any security personnel or police officers were killed.
The Taliban has claimed credit for other recent attacks. But a Taliban spokesman, Zabiullah Mojahid, denied responsibility for this rampage in a tweet, saying: "Today's attack on hospital in Kabul has nothing to do with the Mujahidin of Islamic Emirate," using the group's formal name.
In the vacuum of a Taliban claim, Amaq said ISIS claimed responsibility. Though it is credible that ISIS planned and carried out the attack, CNN has not independently verified the claim.
This is not the first attack at the hospital named after Afghanistan's first president. In May 2011, suicide bombers got inside, and killed six people
and injured 26 others. The Taliban claimed responsibility.
Explosions, then gunfire
Witnesses told CNN an explosion was first heard at 9 a.m. local time (11.30 p.m. Tuesday ET).
Afghan National Police special forces rushed in. "At first there was a firing followed by a huge blast," an employee at a nearby hospital said.
An employee at an Italian restaurant nearby said she heard one explosion, then heard gunfire about 25 minutes later.
The attackers were not immediately killed because security forces were busy evacuating patients, the defense ministry statement said.
US Army Gen. John Nicholson, commander of Resolute Support and US Forces in Afghanistan, said the attack "is an unspeakable crime." He praised security forces for the swift response, saying the forces deserve "our highest praise and respect."
Afghan President Mohammad Ashraf Ghani
said the attack shows terrorists "don't follow rules and laws."
"According to international humanitarian laws, hospitals are immune from attacks," he said.
Afghan Chief Executive Abdullah Abdullah tweeted: "I condemn the terrorist attacked on hospital in Kabul. While we work for peace, we'll avenge the blood of our people."
The US Embassy in Kabul said, "Targeting a medical facility providing care for the brave Afghans working to protect their fellow citizens has no possible justification in any religion or creed."
NATO forces in Afghanistan indicated that the organization was standing by to assist Afghan security forces, according to tweets from Operation Resolute Support.
"Once again insurgents show complete disrespect for humanity by attacking a hospital. We stand with Afghan people against terrorism."
Militants have long targeted loosely guarded targets in Kabul and across Afghanistan. Last month, at least 20 people died after a suicide blast outside Afghanistan's Supreme Court
in Kabul, police and other officials told CNN.
A suicide bomber detonated his explosives in a parking lot near the court in the Afghan capital, according to Basir Mojahid, spokesman for Kabul's chief of police.
Earlier in the year, a spate of attacks
-- two suicide bombings near the Afghan Parliament in Kabul, an explosion at a Kandahar province government compound and a suicide bombing in Helmand province -- left dozens of people dead and wounded.
The Taliban claimed responsibility for the Kabul attacks, which killed at least 36 people and injured 76 others in the capital.
Last summer, seven students, three police officers and two security guards were killed in the attack on the American University of Afghanistan campus
in the capital.
Police searched the university's grounds and killed two attackers who stormed the campus with guns and explosives, Fraidoon Obaidi, chief of Kabul police's criminal investigation department, said. The gunmen detonated explosives and fired guns, witnesses said, causing some students and faculty to flee.