Mass graves containing the bodies of more than 40 civilians allegedly massacred by Taliban and Daesh militants were found after Afghan forces recaptured the village where they died, officials said on Tuesday. 

In a rare joint operation by the rival militants, fighters killed dozens of men, women and children in Mirzawalang, a mainly Shia village in Sayad district of northern Sar-e Pul province, on August 5 after overrunning a government-backed militia, according to officials and residents. Amani said that 23 bodies had been found in one grave, 11 in another and eight in a third.

"The majority have been identified as civilians, while there might be a small number of uprising forces too among them. Search operations will continue tomorrow," he added.

The discovery came after a spokesman for the Afghan National Armyinfo-icon's northern military corps said troops had retaken the village following several days of intense fighting that left at least 50 insurgents dead.

"Our forces are in full control of the village and are searching for Taliban mines and booby traps," Nasratullah Jamshidi said.Dawlat Waziri, a spokesman for the Afghan defence ministry, said forces had "pushed the Taliban out of the village" on Monday afternoon.

He said the militants had suffered "heavy casualties". There has been no word on any casualties to Afghan forces.

Survivors fleeing the violence described nightmarish scenes of militants going from house to house, gunning down local people.

The insurgents also took a number of residents captive, but later released 235 hostages after elders and provincial officials struck a deal with the group.

Daesh has claimed responsibility for killing 54 Shia villagers in Sar-e Pul in a statement released by its propaganda outlet Amaq late Monday.

The Taliban earlier claimed capturing the village but said it did so alone. It also denied allegations it had killed civilians.

Taliban and Daesh militants have regularly clashed since the latter gained a foothold in the country in 2015 but security sources say they have teamed up to attack Afghan forces on a few occasions.