Gunmen dressed in white lab coats stormed a hospital in the Afghan capital and battled security forces for hours, killing more than 30 people and wounding dozens.

Defence ministry officials said the attack started at 9am with a suicide bombing at the front gate of the Sardar Daud Khan hospital in the Wazir Akbar Khan area, central Kabul, on Wednesday.

It was the latest in a series of attacks against civilian and military targets in Afghanistan, and underlined the growing threat posed by anti-government fighters.

The gunmen took positions on the upper floors of the hospital and engaged special forces sent to the scene.

Dawlat Waziri, spokesman for the defence ministry, told Al Jazeera three attackers with automatic weapons and hand grenades entered the complex, the largest of its kind in the Afghan capital.

"Our security forces engaged but they were also careful to not cause any casualties. It was a difficult situation," Waziri said.

READ MORE: US military chief offers assurances to Afghan leaders

Security forces blocked off the area around the hospital, near a busy traffic intersection. As fighting went on, a second explosion struck inside the hospital.

Some patients climbed out of the building and could be seen sheltering on window ledges visible from outside the hospital, which treats military casualties from across Afghanistan, or jumping.

Majid Mojib, in charge of the hospital's intensive care unit, broke a leg as he plunged from a third-floor window.

"I saw in horror that doctors, patients, everybody was screaming as they were chased by gunmen spraying bullets indiscriminately," Mojib told AFP news agency while receiving treatment in another city hospital.

"Many did not survive. It was a massacre."

The Taliban, which has staged similar raids in the past, denied responsibility for the attack.

The Islamic State of Iraq and the Levant (ISIL) claimed the attack in a statement carried by its Aamaq news agency.

President Ashraf Ghani condemned the attack, saying 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.

The Wazir Akbar Khan area of central Kabul is heavily guarded and houses several government offices and foreign embassies.

OPINION: Stop targeting humanitarian workers

The attack comes a week after 16 people were killed in simultaneous Taliban suicide assaults on two security compounds in Kabul.

The country is bracing for an intense fighting season in the spring as the government's repeated bids to launch peace negotiations with the Taliban have failed.

Kabul last month endorsed US general John Nicholson's call for thousands of additional coalition troops in Afghanistan to fend off the group before the spring offensive.

Extra troops were needed to end the stalemate in the war, Nicholson, the top US commander in Afghanistan, told the US Congress in what could be President Donald Trump's first major test of military strategy.

Separately, the Pentagon this year said it would deploy some 300 US Marines this spring to Helmand province alone.

The Marines will assist a NATO-led mission to train Afghan forces, in the latest sign that foreign forces are increasingly being drawn back into the worsening conflict.