Niger state police spokesman Bala Elkana said on Monday that the casualties were caused after gunmen believed to be ethnic Fulani cattle herders opened fire on farmers praying inside a mosque in Etogi village on Saturday.
"The gunmen opened fire on the worshippers inside the mosque during morning prayers and killed all the 20 worshippers," media outlets quoted Elkana as saying, adding, "They later shot sporadically on the village, injuring eight people."
The spokesman said the shooting was a revenge attack after farmers killed one herder in a clash days earlier.
Police said the herders had settled in the area on the condition that they would give a portion of their harvest to the community every season. The herders, however, reneged on the agreement this year, refusing to pay tax and instead claiming that the land belongs to them, Elkana explained.
"This led to a clash in which one herder was killed. The (latest) attack was to avenge the death."
Police have arrested at least three suspects in connection with the violence.
Deadly clashes between herders, who raise cattle, and farmers in Nigeria over land and water rights are frequent. The resource conflict has worsened in some areas by ethnic and religious tensions.
Fulani’s semi-nomadic, cattle-herding way of life has led to decades of conflict with farming communities across central and northern Nigeria.
The Fulani herdsmen usually encroach on and destroy farmers' lands, especially during the dry season, resulting in clashes with farmers trying to stop the Fulani people from using their farmlands as feeding ground for their cattle.
Efforts by the Nigerian government to end hostilities between the two sides have not been successful.
Authorities last year announced a plan to create grazing reserves for herders across the country to relieve tensions. However, the government was forced to put the plan on hold after farming communities rejected it.