A knife-wielding worker at a Park Slope smoke shop foiled the plans of an armed robber Sunday afternoon — by stabbing him, police sources told The Post.

The 25-year-old clerk knifed the crook around 1 p.m., after the suspect pulled a gun on him while an accomplice acted as lookout outside the store near Carroll Street and Fifth Avenue, according to sources.

“He screamed out, ‘I got stabbed!’ ” a 45-year-old resident, who only identified himself as Joseph, said of the armed punk.

The suspect’s accomplice later tried passing himself off as a good Samaritan simply trying to help a wounded man when cops arrived, witnesses said. It’s unclear if the accomplice was ever caught.

Bassel Tawil, a co-owner of the store, said the armed robber came in pretending to look for “different stuff’’ and that one of his clerks, a family friend whom he calls “cousin,’’ came out from behind the counter to assist who he thought was a customer.

“My little cousin comes around the counter to help him, [the thief] grabs him, puts the gun on him and drags him behind the counter,” said Tawil, who reviewed the episode on the store’s surveillance videos.

But the quick-thinking worker reached for a 7-inch blade kept behind the counter.

“He grabs the knife and stabs him in the stomach,” Tawil said of his employee and the robber.

The stabbed man fled the store, tossing his weapon under a car before crossing the street and finally collapsing on the ground, witnesses said.

Joseph said he was unaware of the botched robbery when he rushed to the aid of the suspect.

“I ran across the street to help apply pressure on the wound until the police and ambulance showed up,” Joseph told The Post. “But I didn’t know at the time they were part of a robbery.”

Meanwhile, the suspect’s accomplice — described as a bearded man in his 20s — was on his phone, witnesses said.

“He was acting like myself — as if we were [both] good Samaritans,” Joseph said.

Another neighbor said, “Cops asked [the accomplice] a couple questions, and he said he didn’t know [the guy who got stabbed] but that he was just trying to help.”

The stabbed man — whom cops only identified as 28 years old — was in stable condition at NewYork-Presbyterian Brooklyn Methodist Hospital.

Charges were pending.