A BART ride on Tuesday afternoon turned deadly for a passenger who was brutally stabbed on the moving train as he stopped a thief from stealing another passenger’s shoes, police and sources said.

The Good Samaritan — identified Wednesday by the Alameda County coroner’s office as 49-year-old Oakland resident Oliver Williams — was killed with his own knife, which was taken from him during a struggle with the assailant, who had boarded the train shoeless and tried to take the shoes off of a passenger who had been sleeping, police and sources said.

BART police described the fatal attack on the Warm Springs-bound train as a fight between two men, but BART spokesman Chris Filippi later revealed that the confrontation was sparked by the attempted theft.

A source close to the investigation told The Chronicle that three people were involved in the incident and the man who intervened was killed. Several sources described the attack, which was recorded on the train’s surveillance video, as especially brutal and bloody, and it was witnessed by several passengers.

The incident occurred just before 1 p.m. as the train pulled into South Hayward Station, police said. The suspect fled and was apprehended about a block from the station after he allegedly tried to steal a car at a dealership and grappled with an employee on a busy street.

Steve Castro, 47, an employee at Elias Motors on Mission Boulevard, said he was showing a Dodge Ram van when a tall, shirtless man snatched the keys from his hand. The two began fighting in the roadway as cars zipped past. When one stopped, the man tried unsuccessfully to open the driver’s and passenger’s side doors.

Castro said the man, who did not appear to have any weapons, eventually pleaded with him to stop fighting and told Castro, “You’re going to kill me.”

“I told him, ‘Give me the keys, and I’ll let you go,’” Castro said.

The suspect walked across the street toward a bus stop, and officers arrested him on Tennyson Road based on a description given by witnesses to the BART stabbing, police said. Police did not identify the suspect, but public records show that Jermaine Brim, 39, of San Francisco, was arrested on suspicion of first-degree murder and carjacking in Hayward at 2:30 p.m. and booked into Alameda County jail at 10:08 p.m.

Castro said if he had known the man he fought off had just stabbed a person, he would not have struggled to retrieve the car keys.

“I don’t know what a hero feels like,” he said. “I just took the situation I was in and dealt with it.”

Williams died on the train, which was held at the station during an investigation that disrupted service there for four hours. Officers found him bleeding from stab wounds.

Robert Powers, general manager of BART, said the agency will increase the presence of police officers, community service officers and fare inspectors throughout station platforms and on trains in response to the incident.

“We are heartbroken that a person has lost his life due to violence on one of our trains this afternoon. We extend our sincerest condolences to the family and friends of the victim,” Powers said in a statement. “BART is taking immediate steps to address concerns many of our riders may have as a result of the tragic death.”

At a news conference Tuesday, interim BART Police Chief Ed Alvarez said homicides are rare for the agency.

“This is something that’s very tragic. It’s rare on BART,” Alvarez said. “It’s not something that we get a lot of. I do want to assure our ridership that BART is safe.”

Several BART board directors were stunned and shaken when they learned of the slaying aboard a train on Tuesday afternoon.

Debora Allen, a BART board director, called out her fellow board members for using “social justice excuses” to avoid taking urgent action to keep criminals, mentally ill people and drug users from creating mayhem in the transit system.

“How many more innocent riders have to die violently on BART trains at the hands of mentally challenged people before the Board of Directors directs the GM to take swift, meaningful action?” she said.

Tuesday’s incident devastated an agency that was steadily making progress since last year’s fatal attacks in July 2018, when three people were attacked and killed at BART stations in the span of five days.

Nia Wilson, an 18-year-old woman, was killed and her sister was injured when a 27-year-old transient allegedly slashed them in the neck with a knife in a vicious, unprovoked attack on the MacArthur Station platform.

The day before, 47-year-old Don Stevens died after he was punched and struck his head on the pavement at the Bay Fair Station in an altercation caught on surveillance cameras.

The third victim was 51-year-old Gerald Bisbee, who died from an infected leg wound he sustained when attacked at BART’s Pleasant Hill Station.

Since then, BART police vacancies have dropped from 25 last year to 10 this year. When the Board of Directors approved the 2020 budget in June, they added funding for 19 new officers. The board is also contemplating an ambassador program, in which non-sworn staff would walk through stations and trains, creating a more welcoming environment.

“This is a tragic situation,” board President Bevan Dufty said Tuesday. “The men and women who work for this agency are deeply affected. We’re going to do everything we can to ensure that our riders know how important their safety is.”

Officer Keith Garcia, president of BART’s police union, said the transit agency’s security issues are in part to blame.

“This is yet another case of our system being too porous, and people fare-evading,” Garcia said.

Anna Bauman, Rachel Swan and Alejandro Serrano are San Francisco Chronicle staff writers. Email: anna.bauman@sfchronicle.com; rswan@sfchronicle.com; alejandro.serrano@sfchronicle.com