A man who fired a gun that prompted two Chicago police officers to investigate on train tracks where they were struck and killed by a commuter train told detectives he'd found the weapon in an alley and took it to a remote area to see if it worked, police and prosecutors said Thursday.

Edward Brown, 24, was ordered held on $200,000 bond Thursday during his first hearing since he was charged with felony reckless discharge of a firearm and aggravated unlawful use of a weapon.

At the hearing, Assistant State's Attorney Guy Lisuzzo said Brown had gotten off a train from his job in downtown Chicago to the South Side where he lived, and during his walk home ducked into an alley to urinate.

"The defendant noticed that there was a black fanny pack laying near the garbage cans," Lisuzzo said. Brown looked inside and found a handgun, magazines and ammunition. He then tossed the fanny pack and took the gun home.

Lisuzzo said Brown then took the handgun to the tracks to shoot it.

Brown's attorney, Frank Kostouros, said his client never should have taken the gun, but told the judge that by bringing it to the area of the tracks on the far South Side, "he thought he was doing the something safe."

Prosecutors said Brown fired the gun twice — first into the air and then in the direction of a school that was not in session. Nobody reported the shots to police but the sound was picked up by a ShotSpotter sensor that alerts police when it detects the sound of gunfire.

Officers Conrad Gary and Eduardo Marmolejo were dispatched to the area to investigate. When they arrived, according to prosecutors, Brown saw them and ran back up an embankment he had just walked down. It's not clear if the officers ever saw him as they went onto the tracks to get a better look around. Moments later, two trains passed each other at the spot where they stood.

Video recovered from Marmolejo's body camera, police said, showed the officers were looking toward a northbound train when they were struck by a southbound train they hadn't noticed.

"They must have thought the sound they heard was the northbound train (and) they must have missed the sound of the train right behind them," Chicago police spokesman Anthony Guglielmi said.

Other officers spotted Brown a short time later. Brown was immediately cooperative, prosecutors and police said, and without hesitation admitted that he was carrying the gun he'd found and that he had just fired it. He was arrested and, on the way back to the station, the officers and Brown learned that Gary and Marmolejo had been killed.

Because prosecutors said he was committing a crime when he fired the gun, there was a question about whether he would be charged with murder as has happened to people whose crimes are believed to have led to someone's death. Such charges were filed against a 19-year-old in Alabama this month after he allegedly fired a gun that prompted a girl to run into a street where she was struck by a hit-and-run driver.

Prosecutors declined to discuss their decision not to charge him with a more serious crime, but Guglielmi said the department agrees that the charges are appropriate. Kostouros agreed.

"The nexus between my client shooting up in the air and all the things that had to happen for those officers to lose their lives, it was completely unforeseeable. If he had just gone to the next garbage can over, this never would have happened," Kostouros said.

Brown faces up to three years in prison, if convicted.

Gary's funeral will be held Friday, with Marmolejo's funeral scheduled for Saturday.