Militants wearing suicide vests stormed a Pakistani police academy in the southwestern city of Quetta overnight, killing at least 59 people, mostly police cadets and recruits, and waging a ferocious gunbattle with troops that lasted into early hours Tuesday.

Pakistani officials feared the death toll could rise further, as the four-hours-long siege — one of the deadliest attacks on Pakistan's security forces in recent years — left 117 wounded, some of them in critical condition.

The assault caught many of the recruits asleep in their dorms and forced cadets and trainers to jump off rooftops and run for their lives to escape the attackers.

By mid-day Tuesday, conflicting claims of responsibility emerged. ISIS, which is waging war in Syria and Iraq where it has declared a self-styled caliphate, posted a claim on the group's media arm, the Arabic-language Aamaq news agency. It said three ISIS fighters killed 60 police recruits in Quetta but the claim was not confirmed by Pakistani officials and ISIS did not offer any previously unknown details about the assault.

Earlier, a little-known breakaway faction of the Pakistani Taliban, known as the Hakimullah group, also issued a statement claiming responsibility for the attack. Pakistani officials, doubting the group's capabilities in staging such a co-ordinated and spectacular assault, also could not confirm that claim.

While most of the casualties were police cadets and others at the academy, some of the army personnel who responded to the assault were also among those killed, said Shahzada Farhat, police spokesman in Quetta, the capital of Baluchistan province.

Pakistani family members of victims mourn Tuesday outside a police training center where gunmen opened fire in Quetta. (Arshad Butt/The Associated Press)

Militants stormed the academy shortly before midnight, gunning down sleeping police recruits and waging a ferocious gunbattle for four hours before blowing themselves up.

There were disparate figures as to the number of attackers. Provincial police chief Ahsan Mahboob said there were four gunmen while a statement issued by the military put the number of attackers at up to six.

About 700 cadets, trainees, instructors and other staff were inside the academy when it was attacked, said the home minister for Baluchistan, Sarfaraz Bugti. He added that the gunbattle with the militants lasted for at least four hours.

Pakistani rescue workers transport a body Tuesday from the attack in Quetta. (Banaras Khan/AFP/Getty Images)

Once inside the academy grounds, Pakistani media said the gunmen headed straight to the dorms housing the cadets and trainees and opened fire, shooting indiscriminately. Some of the cadets jumped off the rooftops and through windows to try to escape.

"They were rushing toward our building, firing," one cadet told Pakistani Geo TV news channel. "We rushed for safety toward the roof and jumped down in the back of the building."

Another recruit, his face covered in blood, told the station the gunmen shot at whoever they saw.

"I ran away, just praying God might save me," he said.

After hours of siege, the attack was over, said Bugti. Pakistani forces tightened security around the academy and Quetta hospitals where the wounded were taken.

Some injured falling from roof, walls

Footage aired on local television stations showed ambulances rushing out of the main entrance of the academy as fire engines struggled to put out fires set off by the explosions from the attackers' suicide vests. Most of those being treated at the city hospitals had gunshot wounds, although some sustained injuries jumping off the rooftop of the hostel housing the cadets to escape the gunmen.

"This war isn't over," said Pakistani Interior Minister Chaudhry Nisar Ali Khan. "The enemy is weakened, but not eliminated."

Maj. Gen. Sher Afgan, head of the Pakistani paramilitary force which is primarily responsible for the province, claimed the attackers had received instructions from commanders in neighbouring Afghanistan. He said they were most likely from the banned Lashker-e-Jhangvi Al-Almi militant group affiliated with al-Qaeda and the Taliban. The Sunni militant group has mainly targeted minority Shiite Muslims whom its members consider to be infidels.

Most of those being treated at city hospitals had gunshot wounds, although some sustained injuries jumping off the roof of the hostel and climbing a wall to escape the gunmen. Nearly all of the wounded were police; two were paramilitary troops, authorities said. (Jamal Taraqai/EPA)

Afghanistan dismisses allegations

Afghanistan condemned the attack in Quetta and dismissed Pakistan's allegations that the assault was planned from bases inside Afghanistan.

"Afghanistan is the biggest victim of terrorism and denounces all terrorist attacks," said Mohammad Haroon Chakhansuri, spokesman for Afghan President Ashraf Ghani.

In a separate statement, Ghani also condemned the attack, saying that "terrorism is a threat throughout the region, which is reflected in the brutal act today in Quetta."

Pakistani security forces stand guard outside the Balochistan Police Training College in Quetta the day after militants attacked the training college. (Mazhar Chandio/Anadolu Agency/Getty Images)

Pakistan maintains that militants fleeing army operations in the tribal regions regularly escape across the border, finding safe havens inside Afghanistan. For his part, Ghani has been deeply critical of Pakistan, saying it has provided safe havens to the Taliban and in particular the violent Haqqani network.

For over a decade, Baluchistan has been the scene of a low-intensity insurgency by nationalist and separatist groups demanding a bigger share in the regional resources. Islamic militants and Sunni sectarian groups also have a presence in the province.

Pakistan has carried out several military operations against militants in country's lawless tribal regions along Afghanistan border, including a major push that started mid 2014 in North Waziristan, a militant base. The Islamic militants have killed tens of thousands of people in their bid to overthrow Pakistan's government and install their own harsh brand of Islamic law.

Later Tuesday, a roadside bomb killed a police official escorting a polio team that was travelling in northwestern Pakistan as part of a vaccination campaign, according to Furqan Bilal, a police superintendent in Peshawar. Militant attacks on polio teams are common in Pakistan as Taliban and other extremists denounce such vaccination campaigns as a Western conspiracy.