A California appeals court on Friday reversed a conviction against a Mexican man in the country illegally who was acquitted in the 2015 murder of Kate Steinle in San Francisco.

Jose Ines Garcia Zarate, 46, did not get a fair trial when he was convicted of being a felon in possession of the gun used in the shooting, the Court of Appeal for the First Appellate District ruled Friday.

The San Francisco Superior Court judge who oversaw the murder case disallowed his argument that he never had legal possession of the deadly weapon.

"Because the error was prejudicial, we are compelled as a matter of law to reverse," the appeals court said in its ruling.

A portrait of Kate Steinle is displayed at a memorial site on Pier 14 on July 17, 2015 in San Francisco. Paul Chinn / San Francisco Chronicle via AP file

Garcia Zarate initially told police he was sitting in a chair on San Francisco's Pier 14 on July 1, 2015, when he inadvertently stepped on the gun, which was wrapped in rags, and it discharged.

"There was a, a rag and stuff and I stepped on it and then it fired and then I grabbed it and then tossed it," he said, according to the appeals court ruling.

He later said he picked up the gun and it went off. He tossed it into the bay because he was afraid it would go off again, he said.

"Under defendant’s theory of the case here, he sat in a chair, bent over to pick up an object wrapped in rags, did not know that the object was a gun, the gun fired, and as soon as it fired, he immediately threw the gun in the water in order to stop it from firing," the appeals court found. "If believed by the jury, these facts describe an accidental discovery and abandonment."

Steinle,32, was walking on the pier with her father and a friend when she was struck in the back by the bullet and died. There was evidence, according to the appeals court ruling, that the fatal round ricocheted off concrete before striking Steinle.

Garcia Zarate had been deported five times and had served time in federal prison for re-entering the country illegally. He was facing deportation at the time of the shooting.

San Francisco public defender attorney, Francisco Ugarte answers questions after a verdict was reached in the trial of Jose Ines Garcia Zarate on Nov. 30, 2017, in San Francisco. Marcio Jose Sanchez / AP file

Then-presidential hopeful Donald Trump said Garcia Zarate was the type of person he wanted to keep out of the country. Trump tweeted two days after the shooting that Steinle "was viciously killed b/c we can't secure our border? Stand up for US."

Trump has blamed sanctuary cities like San Francisco, where police are generally forbidden from enforcing federal immigration law, as a reason why felons are in the U.S. illegally.

On Friday night U.S. Citizenship and Immigration Services acting director Ken Cuccinelli echoed the president's earlier criticism, tweeting, "Kate Steinle was tragically killed because San Francisco proudly proclaims itself a sanctuary city."

"How many more innocents will die b4 sanctuary cities stop harboring violent criminals?," he said. "This defies common sense, public safety, & human decency. #NoJustice."

Garcia Zarate was acquitted in 2017 of first-degree murder, second-degree murder, involuntary manslaughter and assault with a semiautomatic firearm in Steinle's death. After the judge denied the jury's request to clarify the definition of illegal possession, he was found guilty on one count, being a felon in possession of a firearm.

He was sentenced to time served, which came to two years, but he was immediately arrested by U.S. Marshals on federal charges of being a felon in possession of a firearm and ammunition and being an “illegally present alien” in possession of a firearm and ammunition.

His attorney, Tony Serra, said that the federal trial was scheduled for Jan. 13 and that the appeals court ruling does not preclude the state court from retrying Garcia Zarate on the illegal weapons possession charge.