HAWIJA, Iraq — A shepherd heard the gunshots and the screaming.

As Islamic State fighters executed at least 60 people at a remote military base in northern Iraq one day last year, he cowered in his home nearby, terrified. When it was safe to go out, he found piles of bodies; many of them he recognized as his neighbors. He buried them himself.

“They were slaughtering people for all kinds of reasons, those caught using the internet, those suspected of being witches,” the shepherd, Saad al-Omar, said at the site of a mass grave here on Tuesday. “Here, I saw those victims, my neighbors. I saw bodies of mothers with their children. They had been shot. They had been burned. They were all dead.”

That executions routinely took place in his hometown, Hawija, was well known, proudly publicized by the Islamic State, also known as ISIS, during its two-and-a-half year rule in the town. But Mr. Omar’s grisly knowledge represents something rare in the quest for justice — a witness who can lead the authorities to the bodies and identify many of them.

As Iraqi officials declared Friday that they had taken the last Iraqi town held by the Islamic State, reducing the group’s self-declared caliphate to a tiny remnant on the Syrian side of the border, they are still trying to comprehend the scale of the group’s horrors.