The gun that killed 32-year-old Kathryn Steinle while she was walking on a San Francisco pier last week belonged to a federal agent, a law enforcement official told the Associated Press Tuesday. The weapon was identified by its serial number.

Juan Francisco Sanchez, an undocumented Mexican felon, was charged with her murder Tuesday and held on $5 million bail. Sanchez told KGO-TV that he found the gun under a bench wrapped in a T-shirt and that it went off accidentally when he picked it up. "Then suddenly I heard that boom boom, three times," Lopez-Sanchez said.

Steinle never exchanged any words with Sanchez. She had been posing near the pier for photos when she was randomly gunned down.

Sanchez originally told the police he was shooting at sea lions. He told KGO-TV that he was high on sleeping pills and marijuana at the time of the shooting.

It is "very likely this was an accidental shooting," said his lawyer, Matt Gonzalez, the city's No. 2 public defender.

"This was an act of random violence, shooting an innocent victim in the back," said prosecutor Dianna Garcia Tuesday, arguing against bail.

Sanchez fought back tears and kept his head bowed throughout the hearing, according to reports, and replied "not guilty" in English and Spanish to every question the judge asked him.

RELATED: DHS blames sheriff's office for illegal immigrant shooting of San Fran woman

Sanchez said he wants the harshest punishment so he can show Steinle's parents he "no longer wants to live," KGO-TV reported.

Sanchez has a nonviolent criminal record and only a second-grade education, his attorney said outside court.

The five-time deportee's confession has touched off a national debate on immigration and San Francisco's "sanctuary city" policy. Sanchez had a long criminal history from 1991 on, including seven nonviolent felony convictions and four narcotics charges.

In March, federal officials transferred Sanchez to jail in San Francisco for a 20-year-old marijuana charge, after his latest federal prison stint for entering the country illegally, reports the AP.

Citing the city's "sanctuary city" policy, the San Francisco sheriff released Sanchez in April after prosecutors dropped the 20-year-old drug charge. The city ignored an Immigration and Customs Enforcement (ICE) request to hold Sanchez so deportation proceedings could begin.

"ICE places detainers on aliens arrested on criminal charges to ensure dangerous criminals are not released from prisons or jails into our communities," said Gillian Christensen, an ICE spokesperson.

San Francisco city law required them to ignore ICE detainer requests Sheriff Ross Mirkarimi said Monday. He said ICE could have obtained a warrant or court order to keep Sanchez in custody and that the agency "knew where he was."

San Francisco Mayor Ed Lee's office is trying to determine if there is a way it can cooperate with Homeland Security and maintain its status as a sanctuary city.

"Mayor Lee shares the senator's concerns surrounding the nature of Mr. Sanchez's transfer to San Francisco and release," said the mayor's spokesperson Christine Falvey. "As the mayor has stated, we need to gather all of the facts as we develop potential solutions."

In the wake of the "sanctuary city" controversy, Sen. Dianne Feinstein, D-Calif., asked Lee to start cooperating with federal immigration officials' deportation efforts.

RELATED: Feinstein calls on San Francisco to comply with federal immigration laws

"I strongly believe that an undocumented individual, convicted of multiple felonies and with a detainer request from ICE, should not have been released," said Feinstein, a former mayor of San Francisco, reported AP. "We should focus on deporting convicted criminals, not setting them loose on our streets."