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

The four-hour siege -- one of the deadliest attacks on Pakistan's security forces in recent years -- also wounded 123, mainly police trainees but also some paramilitary troops, according to Wasay Khan, a spokesman for the elite Frontier Corps. Some of the wounded were reported to be 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.

Pakistani troops responding to the assault said it was over after all three suicide bombers involved in the attack were killed -- one was gunned down while two others blew themselves up.

Later Tuesday, conflicting claims of responsibility emerged. The Islamic State group, 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 IS fighters killed 60 police recruits in Quetta but the claim was not confirmed by Pakistani officials and IS 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 cadets and others from 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.

The attack began at 11:30 p.m. on Monday, said Baluchistan Home Minister Sarfraz Bugti, with three militants shooting and killing a police guard at the watch tower before storming into the academy, located on the city's outskirts.

Baluchistan officials had earlier received "intelligence reports that some terrorists have entered the province" but had no indications about possible targets.

"We had tightened security, which is why they could not do it in the city and chose a target on the outskirts," said Baluchistan's chief minister, Sanaullah Zehri.

There were initially also conflicting police and military statements about the number of attackers involved. About 700 cadets, trainees, instructors and other staff were inside the academy when it was attacked, Bugti said.

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 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 the attack, Pakistani forces tightened security around the academy and Quetta hospitals were the wounded were taken. 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 were injured 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-Qaida and the Taliban. The Sunni militant group has mainly targeted minority Shiite Muslims whom its members consider to be infidels.

The paramilitary chief spoke before the Islamic State and the Hakimullah group's claims surfaced.

Afghanistan condemned the attack 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."

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 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.

Associated Press Writers Munir Ahmed, Zarar Khan and Asif Shahzad in Islamabad; Riaz Khan in Peshawar; Ishtiaq Mahsud in Dera Ismail Khan, Pakistan, and Amir Shah in Kabul, Afghanistan, contributed to this report