A total of 61 people have died after militants wearing suicide vests stormed a police academy in the southwestern Pakistani city of Quetta, sparking a four-hour gun battle.
The 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 to escape the attackers.
Pakistani troops responding to the assault said all three suicide bombers involved in the attack were dead - one having been shot dead while the two others had blown themselves up.
There were conflicting reports over which militant group was responsible. Islamic State (IS) said three of its 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 serious 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.30pm local time 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 in the academy when it was attacked, Mr Bugti said.
Pakistani media said that once inside the academy grounds, 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.
"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 Shia Muslims whom its members consider to be infidels.
The paramilitary chief spoke before the IS and Hakimullah group claims surfaced.
“Islamic State fighters” had carried out the attack, according to the group’s Amaq news agency.
In August, Islamic State claimed responsibility for an attack on a gathering of mourners at a hospital in Quetta that killed 70 people.
But that attack was also claimed by Pakistani Taliban faction Jamaat-ur-Ahrar.
“I saw three men in camouflage whose faces were hidden carrying Kalashnikovs,” one cadet told reporters. “They started firing and entered the dormitory but I managed to escape over a wall.”
Maj Gen Afgan said the attackers may have had inside help, although he did not give details. "This is an open war and when you have enemy inside and outside, they can easily exploit the situation," he said. Security forces and police were clearing the area and searching for any possible hidden attackers.
Many of the trainees were killed when the gunmen detonated explosive vests, he said.
Zarak Khan, a rescue worker from the Edhi foundation, said rescuers initially collected the wounded among security personnel as they fought their way into the compound. The 14km distance between the hostel and the Quetta hospital hampered the rescue effort.
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.
The Dawn newspaper website reported exchanges of fire between security forces and attackers, with explosions heard around the area. Some local media outlets also ran unconfirmed reports that hostages had been taken.
Mobile phone services in the Saryab area shut down shortly after the attack began.