KABUL -- A bomb exploded near a girls' school in a majority Shiite district of west Kabul on Saturday, killing at least 30 people, many of them young pupils between 11 and 15 years old. The Taliban condemned the attack and denied any responsibility.

Ambulances evacuated the wounded as relatives and residents screamed at authorities near the scene of the blast at Syed Al-Shahda school, in the Dasht-e-Barchi neighbourhood, Interior Ministry spokesman Tariq Arian said. The death toll was expected to rise further.

The bombing, apparently aimed to cause maximum civilian carnage, adds to fears that violence in the war-wrecked country could escalate as the U.S. and NATO end nearly 20 years of military engagement.

Residents in the area said the explosion was deafening. One, Naser Rahimi, told The Associated Press he heard three separate explosions, although there was no official confirmation of multiple blasts. Rahimi also said he believed that the sheer power of the explosion meant the death toll would almost certainly climb.

Rahimi said the explosion went off as the girls were streaming out of the school at around 4:30 p.m. local time. Authorities were investigating the attack but have yet to confirm any details.

One of the students fleeing the school recalled the attack. the screaming of the girls, the blood.

"I was with my classmate, we were leaving the school, when suddenly an explosion happened, " said 15-year-old Zahra, whose arm had been broken by a piece of shrapnel.

"Ten minutes later there was another explosion and just a couple of minutes later another explosion," she said. "Everyone was yelling and there was blood everywhere, and I couldn't see anything clearly." Her friend died.

While no one has claimed responsibility for the bombing, the Afghan Islamic State affiliate has targeted the Shiite neighbourhood before.

The radical Sunni Muslim group has declared war on Afghanistan's minority Shiite Muslims. Washington blamed IS for a vicious attack last year in a maternity hospital in the same area that killed pregnant women and newborn babies.

In Dasht-e-Barchi, angry crowds attacked the ambulances and even beat health workers as they tried to evacuate the wounded, Health Ministry spokesman Ghulam Dastigar Nazari said. He implored residents to co-operate and allow ambulances free access to the site.

Images circulating on social media purportedly showed bloodied school backpacks and books strewn across the street in front if the school, and smoke rising above the neighbourhood.

At one nearby hospital, Associated Press journalists saw at least 20 dead bodies lined up in hallways and rooms, with dozens of wounded people and families of victims pressing through the facility.

Outside the Muhammad Ali Jinnah Hospital, dozens of people lined up to donate blood, while family members checked casualty posted lists on the walls.

Both Arian and Nazari said that at least 50 people were also wounded, and that the casualty toll could rise. The attack occurred just as the fasting day came to an end.

No one immediately claimed responsibility for the attack, and Taliban spokesman Zabihullah Mujahid told reporters in a message that only the Islamic State group could be responsible for such a heinous crime. Mujahid also accused Afghanistan's intelligence agency of being complicit with IS, although he offered no evidence.

The Taliban and the Afghan government have traded accusations over a series of targeted killings of civil society workers, journalists and Afghan professionals. While IS has taken responsibility for some of those killings, many have gone unclaimed.

Afghan President Ashraf Ghani issued a statement condemning the attack, blaming the Taliban even as they denied it. He offered no proof.

IS has previously claimed attacks against minority Shiites in the same area, last year claiming two brutal attacks on education facilities that killed 50 people, most of them students.

Even as the IS has been degraded in Afghanistan, according to government and US officials, it has stepped-up its attacks particularly against Shiite Muslims and women workers.

Earlier the group took responsibility for the targeted killing of three women media personnel in eastern Afghanistan.

The attack comes days after the remaining 2,500 to 3,500 American troops officially began leaving the country. They will be out by Sept. 11 at the latest. The pullout comes amid a resurgent Taliban, who control or hold sway over half of Afghanistan.

The top U.S. military officer said Sunday that Afghan government forces face an uncertain future and possibly some "bad possible outcomes" against Taliban insurgents as the withdrawal accelerates in the coming weeks.

----------

Associated Press photographer Rahmat Gul and video journalist Ahmad Seir in Kabul, Afghanistan and Kathy Gannon in Islamabad, Pakistan contributed to this report.