The Brooklyn teenager who allegedly fired bullets at a cop, and later freed on bail while the officer lay in a hospital, claims he was shot in the back and should sue the city, his lawyer said today.

But Elijah Foster-Bey, who appeared in Brooklyn Supreme Court today, told cops a different story on the night of the shooting — he dropped or threw his gun and it went off accidentally, according to court papers.

Foster-Bey is charged with the October 2010 shooting of plainclothes cop Richard Ramirez who chased the teen into a building on Bradford Street in East New York.

Cops alleged that Foster-Bey fired six shots a Ramirez, hitting him twice in the leg and once in the vest.

Authorities said that had Ramirez’s quick-thinking partner not applied a tourniquet to his leg, Ramirez would have bled to death from a torn femoral artery.

Police Commissioner Ray Kelly blasted Brooklyn Supreme Court Justice Gustin Reichbach for granting Foster-Bey $100,000 bail, which the teen’s family met by putting up a house as collateral.

Foster-Bey’s lawyer, Robert DiDio, said his client was set up by police.

“Police officers dressed in plainclothes shot my client in the back as he was going into the building — before any other shots were fired,” DiDio said. “Police Commissioner Kelly ignored all those facts.”

Brooklyn ADA Lewis Lieberman scoffed at that version of events, noting that Foster-Bey apparently told an equally unlikely tale the night of the shooting.

According to court papers, Foster-Bey told two detectives at the scene that “he had a gun, was being chased by cops, ran into the building the gun went off and he was shot by one of the cops.”

Later, the papers say, he told a detective in the ambulance the “he was running from the cops because he had a gun, he threw the gun and it accidentally went off.”

Foster-Bey, who remains free on bail, is due back in court on Feb. 15.