A bystander was shot and seriously wounded by an NYPD cop in Westchester Friday afternoon when a suspect the officer was buying illegal guns from tried to rob him at gunpoint and the undercover cop opened fire, also wounding the alleged gun dealer, law enforcement sources said.

The officer met a gun seller in the Bronx as part of a long-term investigation into illegal firearms and was told to drive to Mount Vernon, where another crook got into his car and put a pellet gun to the cop’s head and demanded his money, sources said.

The undercover handed over the $2,400 he brought to buy two semi-automatic handguns, then pulled out his own weapon and fired 11 rounds at the would-be robber, 37-year-old Alvin Smothers – hitting him in the arm and shoulder about 4 p.m.

Felix Kumi, 61, who lives nearby on Bedford Avenue and didn’t appear to be involved in the robbery or gun sale, was shot in the chest and arm, sources said.

“During the shooting a bystander who was standing behind the suspect, was also struck by at least two rounds,” said NYPD Chief of Department James O’Neill.

“We are told he is in serious condition, and our hearts and prayers go out to him and his family.”

The gun seller who rode with the undercover cop from the Bronx, Jeff Aristy, 28, ran as soon as the shooting started and is still at large, law enforcement sources said.

Smothers was taken to Jacobi Hospital in the Bronx in stable condition. Kumi was also taken to Jacobi Hospital and is in critical condition. Surgeons operated on him Friday night, police sources said.

The undercover cop was taken to Harlem Hospital for treatment to tinnitus to his ear.

Witnesses described a wild shootout.

“There was a rapid succession of gunfire, ‘Pop pop pop,’ and then there was a pause for three or four seconds and then the shots resume again and then another minute or two later and the helicopter was up and there was cars swarming all of this area,” said witness Joseph Camara, 52.

“Swat cars, Police cars, sirens everything else. I was thinking something must’ve gone wrong, there’s a shooting, my first thought [was] it’s a drug related thing it’s not the best of areas.”

Smothers has past arrests in New Jersey for guns, robbery, and assault, while Aristy has past New York drug arrests.

“The undercover business is perpetually dangerous. It’s obvious to me the seller in this operation set up our undercover to be ripped off and it backfired on them,” said the president of the NYPD detectives’ union, Michael Palladino.

The $2,400 and the pellet gun used in the botched robbery were both found on Smothers, police said.

Additional reporting by Josh Saul