A suicide bomber from Somalias Shabaab insurgents killed 18 police officers and wounded 15 others on Thursday when he blew himself up inside the countrys main police academy, the forces chief said.
Witnesses said the police were gathered in a square ahead of their early morning parade when the bomber attacked in the capital Mogadishu.
The assault is the latest in a decade-old battle by the jihadists to overthrow Somalia's internationally-backed government.
"Eighteen members from the police were killed, and 15 others were wounded, after a suicide bomber blew himself inside the academy," acting police chief General Muktar Hussein Afrah told reporters.
The attacker disguised himself in a police uniform to access the camp, Afrah said.
"Some of the police were already in lines, and others were gathering, when the man in police uniform entered and blew himself up," said bystander Hussein Ali, describing the carnage.
Medics and ambulance teams rushed to take the wounded to hospital and collect the corpses.
Officers said the toll could have been far worse had the attacker detonated his bomb in the centre of the crowd.
"The bomber could have inflicted more casualties if he could have managed to reach the midpoint where most people were," police officer Ibrahim Mohamed said.
Later Thursday, police attended the funerals of some of their colleagues killed in the attack.
The Al-Qaeda-affiliated Shabaab claimed responsibility and put the toll at 27 dead.
"It was martyrdom operation, in which the mujahedeen targeted the police academy camp," a statement posted on a pro-Shabaab website read.
The Shabaab lost its foothold in Mogadishu in 2011 but has continued its fight, launching regular attacks on military, government and civilian targets in the capital and elsewhere.
In October, a huge truck bombing blamed on the Shabaab killed as many as 512 people, levelling buildings in the capital's busy Kilometre 5 neighbourhood.
Since then the United States has increased the frequency of air strikes targeting jihadist leaders.
On November 13, the Pentagon said US forces had killed more than 40 Shabaab and Islamic State fighters over four days.
An operation against a Shabaab camp on November 21 killed more than 100 fighters, according to the US Africa Command.
The increase in US raids comes as AMISOM, the AU's mission in Somalia, prepares to withdraw 1,000 troops from its 22,000-strong force, as part of plans to pull out all soldiers by December 2020.
Washington is worried the reduction will hamper efforts against jihadists.
A UN report last month warned that an Islamic State faction in the north of the country had grown significantly over the past year, carrying out attacks in the Puntland region and receiving some funding from Syria and Iraq.