Gunmen wearing white lab coats have stormed a military hospital in Afghanistan's capital, killing at least 30 people and wounding dozens more.

The attack, which was claimed by the Islamic State group, on the 400-bed military facility, located near two civilian hospitals in Kabul's heavily guarded diplomatic quarter, set off clashes with security forces that lasted several hours.

The brazen assault reflected the capability of militant groups in Afghanistan to stage large-scale and complex attacks in the heart of Kabul, underscoring the challenges the government continues to face to improve security for ordinary Afghans.

General Dawlat Waziri, a defence ministry spokesman, said there were "more than 30 killed and more than 50 wounded" in the attack. Afghan forces battled the attackers floor-by-floor, he added.

The ministry said the attackers were dressed like health workers.

According to Gen Waziri, four gunmen were involved, including two suicide bombers who detonated their explosives vests once the group was inside the hospital.

The two other attackers were shot dead by security forces, the spokesman said. A member of the security forces was killed in the shoot-out and three other security officers were wounded.

Along with the suicide vests, the attackers also had AK-47 rifles and hand grenades, Gen Waziri said.

Obaidullah Barekzai, a politician from southern Uruzgan province, said the attack by the Islamic State group and other similar assaults, especially in the capital, are very concerning.

"This is not the first attack by the Islamic State group, they have carried out several bloody attacks in Kabul," he said.

The assault lasted for several hours, with Afghan helicopters circling over the hospital building and security forces going floor-by-floor in a gun battle with the attackers. By mid-afternoon, the attack was over and a clean-up operation was under way.

Abdul Qadir, a hospital worker who witnessed the attack, said an attacker in a white coat shot at him and his colleagues. Ghulam Azrat, another survivor, said he escaped through a fourth-floor window after attackers killed two of his friends.

IS claimed the attack in a statement carried by its Aamaq news agency.

An affiliate of the extremist group has carried out a number of attacks in Afghanistan in the last two years, and has clashed with the more powerful and well-established Taliban, who carried out another complex attack in Kabul last week.

Mohammad Nahim, a restaurant worker in Kabul, said he worries that IS militants are getting stronger.

"Daesh has no mercy on the humanity," he added, using an Arabic name for the group.

Afghan security forces have struggled to combat both groups since the US and Nato formally concluded their combat mission at the end of 2014, switching to an advisory and counter-terrorism role.

Afghan President Ashraf Ghani condemned the attack during an address in honour of International Women's Day, calling it "an attack on all Afghan people and all Afghan women".

The foreign ministry in neighbouring Pakistan condemned the Kabul attack, describing it as a "heinous terrorist attack" and expressing Islamabad's condolences to the victims.

The acting UN humanitarian co-ordinator for Afghanistan, Adele Khodr, warned in a statement that hospitals, medical staff and patients "must never be placed at risk, and under no circumstances be subject to attack", and urged all parties in the conflict to abide by and "respect all medical workers, clinics and hospitals in compliance with international law".

She said that in 2016, at least 41 attacks on health care facilities and workers were recorded across Afghanistan, adding it was "an appalling catalogue of attacks that ultimately further hinders delivery of essential and life-saving health care to all Afghans across the country".