Attackers targeted the 400-bed Sardar Daud Khan hospital starting with two massive explosions outside the building, officials said.
Gunmen then broke into the hospital grounds, witnesses said.
An affiliate of the Islamic State group, IS-K, later said it had carried out the attack.
Photographs and video footage from Kabul showed a plume of smoke over the area and recorded the sounds of gunfire. A doctor in the building told the AFP news agency he had been sent to seek shelter in a safe room during the attack and could hear guns being fired.
Sayed Ahad told broadcaster EVN that one of the blasts was a suicide attack.
"As an Afghan citizen, I am really tired of this war, suicide and explosions," he said. "How long do we have to endure this misery?"
The Taliban spokesman, Bilal Karimi, told the BBC that fighters from IS-K had entered the compound after detonating the first explosion at the entrance gate.
Mr Karimi said Taliban fighters shot and killed four IS-K attackers and captured one alive.
Taliban spokesman Zabihullah Mujahid meanwhile told Reuters news agency that Taliban special forces dropped by helicopter had stopped the attackers from entering the hospital itself, killing them at the entrance or in the building's courtyard. All the assailants were killed in 15 minutes, he said.
Witnesses quoted by Reuters said they saw two helicopters over the area during the assault. The news agency reports that this would be one of the first times Taliban forces have used aircraft captured from the previous, Western-backed government during an operation.
The attack is the latest to hit Afghanistan since the Taliban seized control in August, after the US withdrew its last troops from the country.
IS-K, which stands for Islamic State Khorasan, has claimed responsibility for a number of attacks targeting civilians and Taliban fighters.
In August, a bombing by IS-K at Kabul international airport in August killed more than 150 civilians and 13 US soldiers.