It wasn’t until he took the photograph on Monday morning that Tom Fox, a longtime photojournalist, realized what he was looking at: A man wearing tactical gear and carrying a military-style rifle was standing outside a federal courthouse in Dallas, looking his way.

Moments earlier, Mr. Fox, who works for The Dallas Morning News, had heard what he thought was a backfiring car and later determined to be rounds fired. Then, around 8:37 a.m., he photographed a security guard and a man in a suit as they ran past him.

Mr. Fox was still piecing together what was happening when he looked through the 70-200mm lens of his camera and photographed the man with a rifle, capturing a jarring, detailed close-up of a gunman in the middle of an attack. The image raced across social media, with many praising the photographer for his quick reaction.

“I’m seeing this guy reaching for something and then he started to get up and look my way and I was like, ‘O.K., I’ve been spotted, so get up and run,’” Mr. Fox said in an interview on Tuesday, a day after describing his ordeal to a columnist at his newspaper.