DUBLIN — A veteran BART police sergeant was accidentally shot and killed by a fellow officer during a search Tuesday afternoon at a robbery suspect’s Dublin apartment — the first officer killed in the line of duty in the transit police department’s 42-year history, authorities said.

The friendly-fire death sent a chill through the law enforcement community, who turned out in large numbers to salute the fallen officer at a Castro Valley hospital. Late Tuesday the Alameda County Coroner’s office confirmed that he was 42-year-old Sgt. Tom Smith Jr., a 20-year law enforcement veteran.

“This is a shocking thing for a BART officer to be killed in the line of duty,” said elected BART Director Gail Murray, of Walnut Creek. “But like all peace officers, our BART police risk their lives when they go out to work in the stations and cities. We feel very saddened for the family, who would not expect (this to) happen to their loved ones.”

Around 2 p.m. Tuesday, a group of four members of BART’s detective unit and several members of Dublin Police and Alameda County Sheriff’s departments, converged on the Park Sierra Apartments, at 6450 Dougherty Road. Wearing bulletproof vests, the group were there to search of an apartment belonging a suspect in several robberies on BART property, authorities said.

The suspect, whom officials did not name, was already in custody and was not inside the apartment at the time, according to Sgt. J.D. Nelson of the Alameda County Sheriff’s Office, which is leading the investigation into the shooting.

The officers began their search by knocking twice on the door. Each knock went unanswered, but the door was unlocked, so several of them stepped inside, Nelson said.

Not knowing whether anyone was inside the apartment, the officers followed protocol and entered with their guns drawn, Nelson said.

The details of what happened next have not been released, but Nelson said that once inside the apartment, one of the officers fired a single shot from his weapon, wounding Smith, who later died at Eden Medical Center in Castro Valley.

The name of the BART officer who fired the shot was not released. Nelson said that officer had been in law enforcement for more than 10 years.

“It is with a heavy heart that I have to announce the tragic loss of the first BART officer killed in the line of duty,” BART police Chief Kenton Rainey said Tuesday evening at the hospital. “We ask everyone to give us a chance to catch our breath.”

At the hospital, Rainey and BART General Manager Grace Crunican met with Smith’s family. BART officers and sheriff’s deputies saluted as a gurney was rolled out of the hospital with an American flag draping the body, before it was put into a coroner’s van and taken away, flanked by about 10 law enforcement officers on motorcycles.

The shooting rattled residents of the sprawling apartment complex in the East Bay suburb. Rosalia Vazquez, who lives several doors down from where the shooting occurred inside the Park Sierra Apartments, said she wasn’t home at the time. But she knew when she arrived home that something had happened because the apartment door was barricaded.

“It’s pretty quiet usually. It’s the reason I moved here,” Vazquez said. “I’m just in shock. I can’t imagine this happening.”

The death marked the 36th on-duty officer killed by accidental gunfire in California since 1895. The last time a police officer in Alameda County was killed by another officer was in January 2001, when two Oakland police officers shot and killed an undercover detective.

Officer William Wilkins was pursing an auto thief near 89th Avenue when Officers Andrew Koponen and Timothy Scarrott arrived as Wilkins drew his weapon on the suspect. Both officers fired several times, killing Wilkins, after they said he ignored orders to drop the weapon.

The shooting was the latest challenge for the BART system and its police force. Jan. 1 marked five years since BART police officer Johannes Mehserle fatally shot Oscar Grant, who was being detained on a platform at the Fruitvale station.

More recently, the system was shut down by failed contract negotiations that led to strikes that stranded hundreds of thousands of Bay Area commuters in July and October. Two workers were killed during the October strike, when they were struck by a train on its way to a maintenance yard as they performed track inspections in Walnut Creek.

Staff writers Denis Cuff, Lisa Vorderbreuggen, Thomas Peele and Matt O’Brien contributed to this report.