A United Nations investigation has concluded that Taliban and self-proclaimed fighters from the Islamic State of Iraq and the Levant (ISIL, also known as ISIS) group jointly killed dozens of people in Afghanistan in an early August attack that may have amounted to a "war crime".

UNAMA, the body's mission in Afghanistan, said late on Sunday that it had "verified allegations" of at least 36 deaths in the predominantly Shia village of Mirzawalang, which lies in the Sayad district of northern Sar-e Pul province.

"These killings, corroborated by multiple credible sources, constitute violations of international humanitarian law and may amount to war crimes," UNAMA said in its report.

It added that more than half of the killings took place on August 5 when civilians tried to flee the village after fighters had captured the area following a battle with a government-backed militia.

READ MORE: Afghan forces recapture site of village 'massacre'

Afghan officials claimed that Taliban and ISIL fighters killed more than 50 villagers, including by beheadings, in a rare joint operation between the two armed groups.

The UN investigation said that as many as 27 civilians were killed, including one woman, four teenage boys and 13 men over the age of 60.

At least seven pro-government militia fighters, one local policeman, and an Afghan army soldier were also among the dead, UNAMA's report added.

The organisation was unable to confirm the claims of beheadings.

'They beheaded my brother'

Witnesses have described the horror of events to Al Jazeera, detailing how fighters went from house to house shooting villagers.

"Me, my brother Ghulam and my sister-in-law left our home to escape the horror that was happening in the village," Sakhi, a resident of Mirzawalang, told Al Jazeera on August 15. "When we reached the highway, the militants blocking it asked us to get out of the car and started hitting us."

"They beheaded my brother and the others in the car, who were with us trying to escape.

"I grabbed my sister-in-law's hand and ran as fast as I could. They started firing, but we managed to escape," Sakhi said, describing how he also witnessed women and children being beheaded.

"I can't cope with the horror and pain ... we've been through hell."

Another witness, Haji Mahdavi, said fighters shot civilians indiscriminately "like monsters".

READ MORE: Father of robotics team member killed in Herat attack

"There were no questions asked, whether young or old, man or woman, no one was spared. Bullets were firing from all directions. It was a sight of the most gruesome and monstrous act ever," Mahdavi told Al Jazeera on Monday.

UN investigators noted that a commander implicated in the raid had claimed allegiance to ISIL, but concluded they were "not aware of any information supporting his links" to the wider ISIL network.

From clashing to joint attacks

Taliban and ISIL fighters have regularly clashed in Afghanistan over the past two years, but allegiances are occasionally fluid, and security sources say they have teamed up in the past to attack Afghan forces in certain areas.

"It was collective punishment," Sami Yousafzai, an Afghan journalist who has covered the war and Taliban since their emergence, told Al Jazeera. "Both Taliban and the ISIL fighters had one goal, and that was to cause maximum damage to the government forces and buildings in the area, which could be one reason why they conducted a joint attack."

"The important point here is that fighters who now say they are under ISIL were actually Taliban fighters before and have now parted ways and claimed allegiance to ISIL. These fighters personally know each other and have some understanding in between them.

"However, we should all keep in mind that there are fewer than 1,000 ISIL fighters in the country."

The Taliban had earlier confirmed capturing Mirzawalang but said it did so alone. It has also denied allegations it had killed civilians.

Last week, ISIL claimed responsibility for killing 54 Shia Muslims in Sar-e Pul in a statement released by its propaganda outlet, Amaq.

Over the past year, ISIL has carried out a number of deadly attacks on civilians, particularly against the Shia community in Afghanistan.

Earlier in August, ISIL claimed responsibility for a suicide attack which killed more than 33 worshippers at a Shia mosque in the western city of Herat.

With additional reporting by Shereena Qazi: @ShereenaQazi