He first heard the "wails for help" as he walked out of the police station about 10:30 p.m. Saturday.
After jumping in his car, putting down the windows and following the sounds, the detective got closer and heard the sound of "metal hitting an object."
Soon after, Detective Mike Ebaugh of the Prince George's County Police Department allegedly witnessed a man "strike a nearly lifeless man with a shovel to the head," according to police.
Ebaugh jumped out of his car, pulled out his handgun and ordered the man to drop the shovel, just as the man was "poised to hit the victim once again," police said.
The man, identified as David Eugene Hamilton, 48, of the 2000 block of Ray Leonard Road in Palmer Park, listened to Ebaugh and surrendered, police said. Ebaugh then called for back up and emergency medical personnel.
Ebaugh, an eight-year veteran, first heard the "wails" as he left a police district station in the 7800 block of Barlowe Road in Palmer Park, police said.
The department said Ebaugh "stopped a violent assault and likely prevented a homicide."
Hamilton has been charged with first-degree assault.
The victim remained in an area hospital Monday afternoon.