A man was charged Wednesday for firing a gun near Chicago railroad tracks where two police officers were fatally struck by a commuter train, reports said.

Edward R. Brown, 24, was charged with reckless discharge of a weapon and aggravated unlawful use of a weapon, the Chicago Sun-Times reported.

Officers Eduardo Marmolejo, 36, and Conrad Gary, 31, were killed Monday night while trying to locate the person who fired the weapon.

The officers had run onto an elevated area of the tracks on the city's far South Side to investigate the gunfire. A northbound Metra train had approached ahead of them, but it was the South Shore Line train that they hadn't noticed, the Chicago Tribune reported.

When South Chicago District officers saw Brown walk down a set of stairs from the Metra train station, they stopped him and asked him if he was carrying anything illegal, to which he said, “Yes, I have a gun on me,” Chicago Police spokesman Anthony Guglielmi said, according to the Tribune.

Police then found a .38-caliber Kel-Tec semiautomatic handgun on Brown's sweater pocket. The suspect admitted to shooting the gun on the tracks, the local papers reported, citing officials.

At least four shell casings were also recovered, Fox 32 Chicago reported.

Shotspotter technology had detected the sound of gunfire and alerted police about shots fired in the area.

The police officers were focused on the other train coming from the opposite direction, officials said.

"They must have thought the sound they heard was the northbound train," he said. "They must have missed the sound of the train right behind them."

CHICAGO OFFICERS LIKELY DIDN'T SEE TRAIN THAT KILLED THEM

Between the sound of the first train and focus on their investigation, they were unable to move off the tracks.

"These brave young men were consumed with identifying a potential threat," Superintendent Eddie Johnson explained to reporters at a news conference late Monday night.

Chicago Police Officer Eduardo Marmolejo, pictured, and Officer Conrad Gary were fatally struck by a train as they investigated a report of gunshots on the city's far South Side, Monday, Dec. 17, 2018, in Chicago.

Chicago Police Officer Eduardo Marmolejo, pictured, and Officer Conrad Gary were fatally struck by a train as they investigated a report of gunshots on the city's far South Side, Monday, Dec. 17, 2018, in Chicago. (Chicago Police Department via AP)

Guglielmi said "very limited" video from a body camera one of the officers was wearing helped investigators piece together how the tragedy might have happened.

On the video, they "clearly acknowledge" a northbound train just before the southbound train hit them, Guglielmi said.

The Associated Press contributed to this report.