The Metropolitan Police said the accident occurred in the early hours of Friday at Croydon Detention Center in south London.
According to a police statement, fellow officers and paramedics treated the officer at the scene. However, he later died of his wounds in hospital.
Police said a 23-year-old man was arrested at the scene. He is in hospital in critical condition from a gunshot wound, and the Metropolitan Police Department has launched a murder investigation.
The fatal shooting of a police officer is an unusual event in the UK, where the police do not routinely carry weapons.
The police statement said no police firearms were fired during the incident.
“This is a truly horrific incident in which one of our colleagues lost his life in the most tragic circumstances,” said Metropolitan Police Commissioner Cressida Dick. “My heart breaks for his family, colleagues and immediate friends.”
British Prime Minister Boris Johnson said the country owes a “huge debt” to police officers who risk their lives to keep society safe.
“My sincere condolences to the family, friends and colleagues of the police officer who was killed in Croydon last night,” he said in a tweet on Twitter. “We owe a huge debt to those who risk their lives to keep us safe.”
British Home Secretary Priti Patel said she was “shocked and saddened” by the news. “My thoughts today are with his family, friends and fellow police officers in London and across the country,” she said in a statement.
Patel said she offered her condolences to Dick and provided any support required during the shooting investigation.
“This is a sad day for our country and another horrific reminder of how our police officers put themselves in danger every day to keep us safe,” she said.
London Mayor Sadiq Khan said he was “devastated” by the shooting.
“My heart goes out to the family of this courageous officer, who paid the ultimate price for helping to keep Londoners safe. Such tragic incidents are a terrible reminder of the dangers our police officers face every day,” Khan said on Twitter.
“My thoughts are also with the entire Metropolitan Police family, who I know will mourn deeply for their colleague at this extremely difficult time. I remain in close contact with the Commissioner to offer my support to her and to the Met officers and staff.”