Another 50 were wounded in the attack in which gunmen disguised as doctors stormed the hospital and exchanged fire with security forces for several hours.
All four attackers were also killed during the six-hour gun battle, officials said.
The assault on the Sardar Mohammad Daud Khan Hospital began when a suicide bomber blew himself up at the rear entrance to the building.
The other three attackers, carrying automatic weapons and grenades, then entered the complex.
Hospital administrators told the AFP news agency that they saw three attackers wearing white laboratory coats on the loose.
One worker, Abdul Qadir, told the Reuters news agency that he saw one of the gunmen, wearing a white coat, take out an AK-47 assault rifle and open fire, killing at least one patient and one hospital worker.
Security forces blocked off the roads around the building and helicopters circled over the area as the gunmen took positions in the upper floor of the 400-bed hospital.
As the fighting went on, some patients climbed out of the building and could be seen sheltering on window ledges.
The hospital, which treats military casualties from around Afghanistan, is in a neighbourhood that is home to several embassies. The heavily fortified US mission is just across the road from the hospital.
President Ashraf Ghani said the attack “tramples all human values”.
“In all religions, a hospital is regarded as an immune site and attacking it is attacking the whole of Afghanistan,” he added.
IS claimed responsibility in a statement that read: “Infiltrators from the Islamic State attack the military hospital in Kabul.”
The group has mounted several high-profile attacks on civilian targets in Kabul over the past year, including several on prominent Shia targets.
Government officials have also warned that the Taliban are stepping up their insurgency.
Last week they launched an attack in the Afghan capital that killed at least 22 people and set off clashes that lasted several hours.
The Taliban are seeking to expel foreign troops, defeat the US-backed government and reimpose Islamic law.