More than 30 people have been killed after attackers dressed as doctors stormed the largest military hospital in Kabul, Afghan officials say.
Militants armed with guns and grenades gained entry after one detonated explosives at a hospital gate and then opened fire on staff and patients.
Commandos who landed on the Sardar Daud hospital roof killed all four attackers after several hours of fighting.
The so-called Islamic State (IS) group has claimed the attack.
The Taliban has denied any involvement.
More than 50 people were also wounded, the defence ministry said.
President Ashraf Ghani said the attack at the 400-bed hospital “trampled all human values”.
“In all religions, a hospital is regarded as an immune site and attacking it is attacking the whole of Afghanistan,” he said.
The attack began at 09:00 local time (04:30 GMT). One hospital staff member who was able to get out saw an attacker “wearing a white coat holding a Kalashnikov and opening fire on everyone, including the guards, patients and doctors”.
One employee wrote on Facebook: “Attackers are inside the hospital. Pray for us.”
Change of tactic: Analysis by Inayatulhaq Yasini, BBC Afghan
The hospital attack marks a change in approach by so-called Islamic State fighters in Afghanistan – it’s the first time they have engaged directly with security forces in the capital.
Previously they have targeted civilian gatherings, mainly of Shia Muslims, as well as causing carnage at the Supreme Court last month.
But at the hospital they used an approach more commonly associated with the Taliban – blowing the gates open to allow gunmen to enter. This suggests they now have the resources and the military training to expand their attacks.
If that’s the case, the security forces could face more such assaults in the coming months.
In the two years since it announced its presence in Afghanistan, IS has mainly engaged with Afghan forces – and more powerful, rival Taliban fighters – in the east, near the Pakistan border. It has failed so far to widen its base in the country – one reason, observers suggest, it may now be mounting more headline-grabbing attacks.
The government claims it has rooted out IS militants from a number of bases in the east – but has yet to dislodge them from mountainous areas they control.
TV pictures showed people hiding from the gunmen on ledges outside windows on upper floors of the building.
More than six hours after the attack began, interior ministry spokesman Sediq Sediqqi tweeted that special forces had ended their operation and all the attackers were dead.
The IS-affiliated Amaq news agency shared two images via the Telegram messaging app that appeared to show one of the militants taking part in the assault and a number of dead bodies.
Afghanistan’s de-facto deputy leader Abdullah Abdullah also condemned the attack on Twitter and vowed to “avenge the blood of our people”.
IS announced it was moving into Afghanistan and Pakistan when it declared its so-called Khorasan Province in 2015 and has since carried out a number of attacks.
In July 2016, a suicide bomb attack on a rally in Kabul killed about 80 people.
Three months later, two similar attacks during the religious festival of Ashura claimed about 30 lives and in November 2016 an attack at a mosque in Kabul killed more than 30.
IS also claimed a suicide attack at Kabul’s Supreme Court last month that killed 22 people and has stepped up activity in both Afghanistan and Pakistan.
The Taliban has also been carrying out attacks, killing 16 people in Kabul in suicide attacks a week ago, after beginning its Spring offensive early.