KABUL (Reuters) - Gunmen dressed as medics attacked a hospital in the Afghan capital on Wednesday and battled security forces for hours, killing more than 30 people and wounding dozens in an assault claimed by Islamic State.

A suicide bomber blew himself up at the rear of the 400-bed Sardar Mohammad Daud Khan hospital, across the road from the heavily fortified U.S. embassy, providing the signal for three attackers with automatic weapons and hand grenades to open fire inside the complex, according to witnesses.

Defence Ministry spokesman Dawlat Waziri said the attack was suppressed by mid-afternoon, with all three gunmen killed.

As security forces swept the hospital buildings, another ministry spokesman said they found more than 30 dead and 50 wounded, including doctors, patients and hospital staff.

Earlier, a spokesman for the public health ministry said three dead and 66 wounded had been taken to other hospitals in the city.

The gunmen, dressed as medical personnel, had taken up positions on the upper floors of the hospital and engaged special forces sent to the scene, officials said.

Security forces blocked off the area around the hospital, near a busy traffic intersection, and special forces soldiers descended on to the roof of the main building from helicopters.

Sporadic gunfire could be heard for several hours and, as fighting went on, there was a second explosion, which a spokesman said was caused when a car inside the hospital complex blew up.

A statement from Islamic State’s Amaq News Agency said its fighters had attacked the hospital, while an Afghan Taliban spokesman denied responsibility, saying the Islamist insurgency had “no connection” with the attack.

The raid on the hospital followed warnings by government officials that high-profile attacks in Kabul were likely to escalate this year.

With U.S. President Donald Trump yet to announce his policy for Afghanistan, where the top U.S. commander has said thousands more international troops may be needed to maintain stability, the attack also pointed to Islamic State’s growing threat.

The movement, whose local branch is called Afghanistan Islamic State of Iraq and Syria - Khorasan province (ISIS-K) is opposed to both the Western-backed government in Kabul and the Taliban. The movement is based in the Middle East but has established a solid presence on the border with Pakistan.

It has also mounted several high-profile attacks on civilians in Kabul over the past year, including several on prominent Shi’ite targets.

HIDDEN WEAPON

The attack on a hospital that treats military casualties from around Afghanistan drew wide condemnation and President Ashraf Ghani said it “trampled on 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 in impromptu remarks during a speech for International Women’s Day in Kabul.

General John Nicholson, commander of the NATO-led Resolute Support mission, praised Afghan forces which he said had responded “swiftly and professionally.”

Slideshow ( 14 images )

“We are committed to help our Afghan partners destroy ISIS-K in Afghanistan,” he said in a statement.

The United Nations Assistance Mission in Afghanistan said the attack on hospital staff and patients not involved in the conflict amounted to a war crime.

Witnesses inside the hospital said they were caught by surprise as a gunman dressed in a white doctor’s coat took out a concealed AK-47 assault rifle and opened fire, killing at least one patient and one hospital worker.

“Suddenly gunfire broke out and a gunman was shooting at everyone,” said Zahir Khan, who hid under a table and later escaped. “He was shooting at doctors, patients and visitors.”

Slideshow ( 14 images )

As the fighting went on, some patients climbed out of the building and could be seen sheltering on window ledges high above the ground.

Patient Zia Zabuli was lucky to escape. In hospital with a leg wound, he and three others hid in a room and barricaded themselves in when they saw one of the gunmen approaching.

“Together we put beds, chairs and whatever there was behind the door,” he told Reuters after the siege ended late in the afternoon.

At one point, one of the assailants tried to break in.

“He came up to our door and kicked it several times but it did not open. Then he left,” said Zabuli, as he limped away from the scene supported by a relative. “We stayed quiet and prayed for our safety.”

The attack came just a week after dozens of people were killed and wounded in coordinated attacks on a police station and an office of the intelligence service in Kabul.

That attack was claimed by the Taliban, who are seeking to expel foreign troops, defeat the U.S.-backed government and reimpose Islamic law after their 2001 ouster.

Away from Kabul, dozens of people have been killed over the past few days in fighting across Afghanistan, from Kunduz and Baghlan in the north to Farah in the southwest and Helmand and Kandahar on the Pakistan border in the south.