Sher Ahmad Ammar, deputy governor of Herat, said the men had kidnapped a local businessman and his son and intended to take them out of the city, when they were seen by patrols that set up checkpoints around the city.
"Their bodies were brought to the main square and hung up in the city as a lesson for other kidnappers," he said.
The two kidnapping victims were released unharmed, he said.
Herat resident Mohammad Nazir said he had been shopping for food near the city's Mostofiat Square when he heard a loudspeaker announcement calling for people's attention.
"When I stepped forward, I saw they had brought a body in a pickup truck, then they hung it up on a crane," he said.
Footage of the bloodstained corpse, swinging on the crane was widely shared on social media, showing a note pinned to the man's chest saying "This is the punishment for kidnapping".
No other bodies were visible but social media posts said others were hung up in other parts of the city.