DAKAR, Senegal — A mob attacked gay people in a neighborhood in Abuja, the capital of Nigeria, dragging young men from their homes, beating them with nail-studded clubs and whips, and shouting that they were “cleansing the community” of gays, several Nigerian activists and a witness said Saturday.

The attack took place late Wednesday night in the Gishiri neighborhood, and one victim was beaten nearly to death, the witness said. Afterward, the mob of about 50 young men dragged four of the victims to a nearby police station, where the police further beat and insulted them, said the witness, who gave his name as John. His last name is being withheld for his safety.

The attack came in the wake of a new law signed by President Goodluck Jonathan prescribing prison sentences of up to 14 years for gay people. There have been recent episodes of similar mob violence in the Muslim-dominated north of Nigeria.

In Abuja, the witness and the activists said, some in the mob were shouting, “We are working for Jonathan!”