Police officers and members of vigilante groups were among those killed in the attacks.
The police in Katsina and Zamfara states on Thursday said 21 persons, including police officers and members of vigilante groups, were killed in bandits attacks in the states this week.
The police in separate statements said the attacks occurred in Dandume and Maradun local government areas of Katsina and Zamfara respectively.
The police spokesperson in Katsina State, Gambo Isah, said the attack in Diskuru in Dandume LGA where gunmen killed at least 12 locals, four vigilantes and a police officer, was retaliatory.
Mr Isah said the gunmen had been forced to retreat from a previous attack on the community after suffering heavy casualties from the locals who defended their community.
However, the police said the gunmen returned to the community on Thursday in a larger number, on motorcycles, and with more sophisticated weapons. They shot at people indiscriminately and burnt down many homes.
A combined team of security personnel later arrived the community, and with the local vigilante group, engaged the bandits in a gunfight.
The police spokesperson said the gunmen again suffered heavy casualties following a reinforcement from the Air Force who brought in fighter jets. He said the security personnel were still combing the forest to recover corpses of the bandits.
Also on Thursday, the police in Zamfara said four people were killed in an attack by armed bandits at Gidan-Goga village of Maradun Local Government Area.
Three people sustained injuries in the attack, the police added.
The police spokesperson in the state, Muhammed Shehu, said the swift response of the security personnel to distress calls from the village prevented more carnage.
"The bandits fled before the arrival of the joint security operatives. Unfortunately, four people were killed by the bandits while three others were injured. The victims are now responding to treatment at Federal Medical Centre in Gusau.
"The Joint Police/ Military units from Kaura Namoda area led by the DPO Kaura and another combined team of Conventional Policemen/ 78 Mobile Police Squadron in Maradun led by DPO Maradun responded to the distress call and arrived the village to counter the bandits and to forestall further attack on the the neighbouring villages," Mr Shehu said.
He added that security has been beefed up in the area with improved patrol by the joint operation to build the confidence of the community.
The state's Commissioner of Police, Usman Nagogo, condoled with the families of the victims and promised that the police would not relent in their effort to ensure the protection of life and property, Mr Shehu said.
Attacks by armed bandits on communities is prevalent in Northwestern states including Zamfara, Katsina, and Kaduna.
Apart from killing residents and destroying properties, the bandits also kidnap residents for ransom.