NYPD Detective Kevin Herlihy followed the suspect in a brutal shooting into a Harlem subway station, where the career criminal suddenly pointed a .22-caliber pistol in the cop’s direction and fired.

Herlihy then drew his weapon with his right hand and began to return fire — even though a bullet fired by Michael McBride, 52, had hit him in the left arm.

“It didn’t click until I looked at my arm, saw a hole and felt, ‘Oh, man, I just got shot. I’m in a gun battle,’’ recalled Herlihy, who fatally shot McBride from about 10 feet away.

The confrontation began at 4:15 p.m. on Valentine’s Day, in the mezzanine of the St. Nicholas Avenue station at West 145th Street, and “people there cleared out or hit the deck once he started shooting,” Herlihy said.

“Everyone went down, and it was him or me.”

Herlihy and three other members of the Queens Violent Felony Squad — whose task he described as going after “the worst of the worst’’ — had tracked McBride through his cellphone.

He was wanted for shooting Shante Plowden, 25, daughter of his estranged girlfriend, in the head in Queens the night before.

A month or so later, Herlihy visited Plowden.

“I went to give her a hug,” said Herlihy, nomiated for a Liberty Medal in the Finest category.

“It was comforting to know she was doing well, and I was glad I could be there for her.’’