India’s prime minister, Narendra Modi, has condemned a rise in deadly mob attacks on cattle traders, beef-eaters and dairy farmers, saying killing people in the name of protecting cows is unacceptable.

Most of the attacks waged by so-called cow vigilantes from fringe Hindu groups have targeted Muslims. Cows are considered sacred by many members of India’s Hindu majority, and slaughtering cows or eating beef is illegal or restricted across much of the country.

Modi’s comments in the city of Ahmedabad, in his home state of Gujarat, followed protests in several cities on Wednesday night against recent violence targeting Muslims. Thousands of people carrying placards reading “Not in my name” sang songs and lit candles in New Delhi. Hundreds including Bollywood actors rallied in the pouring rain in Mumbai.

The prime minister and others in his Hindu nationalist party have rarely spoken about the killings over the last two years, leading some rights activists to accuse Modi and his government of tacitly supporting the attacks. In 2016, Modi said he felt enraged by those masquerading as cow protectors to commit crimes.

Narendra Modi at the Gandhi ashram in Ahmedabad. Photograph: Ajit Solanki/AP

On Thursday, he warned that “no person in this nation has the right to take the law in his or her own hands”.

He referred to the pacifist freedom fighter Mahatma Gandhi in insisting “there is no place for violence” in society.

He criticised all mob attacks, but made note of the rise in attacks against people employed in the cattle industry and who eat beef. “It is not something Mahatma Gandhi would have approved,” he said.

Modi did not directly refer to Muslims in his remarks.

Last Friday, a group of about 20 men attacked four Muslims on a train in New Delhi’s outskirts, fatally stabbing a teenager and seriously injuring two others. The Muslim men said their attackers accused them of being “beef-eaters” after an argument over seats.

That attack was just one of five such killings in the last three months – almost all of them in broad daylight and in busy public areas.

On 1 April, a Muslim cattle trader was lynched by a mob in the western state of Rajasthan as he was transporting cattle he had bought at an animal fair. A month later, two Muslim men were beaten to death over allegations of cattle theft in India’s north-east.

Muslims constitute about 14% of India’s 1.3 billion people.