It happened at 12:40 p.m. outside a McDonald's on King's Highway and Church Avenue in East Flatbush.
Police say it began when a 67-year-old customer was attacked by the man. Correction officer Michael Turnbull intervened, but the man swung at him with a pipe and Turnbull drew his weapon and shot him in the leg.
Police say Turnbull shot him again in the leg when the attacker continued to swing at him.
The man was taken to Kings County Hospital in stable condition. His identity was not immediately released.
The investigation is ongoing.