Rights worker Olumide Makanjuola says a gay friend agreed to be flogged in an attempt to ‘whip the devil out of him’. Photograph: Sunday Alamba/AP

The party had just started when the gunshot pierced the music. Instantly the men scattered, knowing what it meant: a police raid.

They had gathered in a hotel in the northern Nigerian state of Bauchi, renting out almost a whole floor for a surprise birthday party. But in the minaret-dotted city, where sharia in theory requires gay men to be stoned to death, such stolen moments are fraught. Someone had tipped off the Hisbah – the religious police.

As officials stormed in on that night in 2007, John (not his real name) felt numb with fear. He ran to a room, switched off the lights and crawled under the bed. "They checked room by room. They opened the door and flashed a flashlight, but they thought it was empty." They arrested 18 others.

A week later, John went to Friday prayers at the mosque. He prayed for 18 of his friends who faced sodomy charges in a sharia court. He prayed for their lawyer, who was forced to sneak into the first hearing via a side door as a mob threatened to stone him for defending "gay marriage". He prayed for strength to do what he had decided to do next.

"That incident really gave us the courage to start doing something. We couldn't hide any more," recalls John. And so, in one of the most conservative states in Nigeria, he started holding underground meetings with other gay people. They supported each other when neighbours accused them of being "demons". Sometimes money was pooled together to pay bail or buy condoms, handed out to those who couldn't afford them. Mainly, though, they helped each other cross the lonely horizon of living each day in denial, finding solace in mutual acceptance.

For years, they gathered in secret. But last week Nigeria's president, Goodluck Jonathan, signed the same-sex marriage (prohibition) bill, unleashing a wave of homophobia that threatens to sweep away seven years spent building a fragile haven. The far-reaching law targets not only homosexuals but also those who support their rights, or who fail to report gay people. At least 40 arrests last week swelled the number of those incarcerated to almost 200 across Nigeria, rights groups told the Observer.

One by one, John and his friends fled the city.

"More than 90% of Nigerians are opposed to same-sex marriage. So, the law is in line with our cultural and religious beliefs as a people," said Reuben Abati, the presidential spokesman. The president's approval ratings soared after months of dismal news about corruption, political violence and a radical Islamist insurgency in the north.

From his location in hiding, John thinks about what to do next. "I'm not comfortable here at all. I cannot stay here doing nothing."

In a hotel room in the capital, Abuja, two women in hijabs are visiting Dorothy Aken'ova to buy goods considered contraband: sex toys. Providing a rare place where society women feel comfortable enough to buy roleplay lingerie without being judged is just one way Aken'ova tries to liberate her sexually repressed country. Another is hiring lawyers to defend men or women arrested for being gay.

The mother of three has filled her week with phone calls, trying to find lawyers willing to represent those in detention. One man was arrested after his landlord said it was suspicious he shared a flat with another man.

"The lawyers who accept these jobs will charge the skin on your bum. But then the cost of armed guards to accompany them isn't cheap," Aken'ova sighs, before telling the two giggling women the price for bottles of massage oil.

Money – sometimes out of Aken'ova's own pocket – is no longer the biggest problem. Simply persuading someone to take up cases is much harder, with many fearing they will be targeted by association. "As soon as I mention gender minority rights, people ask me: 'Are you a lesbian?' You can tell they're willing to immediately dissociate with you if you answer in the affirmative," says Aken'ova, whose quick smile blossoms as brightly as the tattooed flower on her right biceps.

Such reactions are common across Africa, where populist bills have cracked down on homosexuality, often tightening colonial-era laws. International pressure against such moves has fuelled anti-gay sentiment, with leaders using anger at perceived western interference as an escape valve. The Ugandan president, Yoweri Museveni, last week said gay people were the product of "random breeding" in the west when "nature goes wrong", but blocked an anti-gay bill after months of pressure from international donors. Unlike Uganda, about half of whose budget is supplied by western donors, Nigeria is flush with petrodollars and can defy such pressure.

For campaigners, the problem starts with the title of the bill. "People read it and think: OK, I agree with this. They don't question what else is inside that bill," says Aken'ova, who has never heard of anyone campaigning for gay marriage. "It's not [just] anti-gay people; it's anti-people."

Last year, a lawmaker said of the bill: "You have a right to your sexual preference but by trying to turn it into marriage do you realise you could be infringing on the human rights of the other person who finds it repulsive?"

So far, they haven't been the victims. Last week Ibrahim Marafaa, a 47-year-old teacher who was arrested before the bill was signed, was publicly flogged and fined 5,000 naira (£20) after "confessing to his abnormality".

"If he feels an injustice has been done, he has the right to appeal within 30 days," said Alhassan Zakaria, the sharia lawyer who oversaw the whipping.

Down south, too, floggings aren't uncommon. Lagos-based rights worker Olumide Makanjuola recounts how a friend of his agreed to be flogged in a bid to "whip the devil out of him". "He just wanted to stop being the subject of hatred," Makanjuola says, very softly.

Earlier he spent an hour talking to family members to reassure them about his safety. Then two friends called to say they're leaving the country. One, a doctor, asked if he could be prosecuted for treating gay patients.

Last year Makanjuola documented a case where four men suspected of being gay were publicly stripped, beaten, tied together and paraded naked in a south-western village. The police said they had no evidence of the incident, captured on camera by a jeering mob, but opened investigations to find out if the men were "sodomites".

Makanjuola refuses to believe the mob's anger was about homosexuality which, he says, was a scapegoat for their desperation in a country where mismanagement and corruption have left most people jobless and poor.

"They're a clear example of people who are frustrated by the system. But they should be directing it at our leaders who are buying houses in London and Dubai using looted funds," he says.

Others have little truck with that argument. "Being gay is due to lack of parental care," says Abdullahi Sani, a policeman who took time off work to attend the lashing in Bauchi. "Twenty lashes is child's play compared to the offence. The victim has ceased to be a normal human being. He has lost sight of God."

It's in this climate John has worked to forge his place in the world. And life was beginning to make sense, he says.

His goal was clear: to act as a point man in a quiet but growing underground movement. This despite his father sitting him down last month and telling him about a gay friend who had recently been beaten up, to stop "associating with that gay boy".

"I'll try but it's not good to suddenly start avoiding a friend. He's a human being," John told him.

Once, his mother, who died last year, took him aside. "She told me: People will always talk. Forget about them. Just be careful and concentrate on your studies," he recalls. "She loved me so much because I was the last-born son," he says, his voice breaking.

John tries to remember that advice now, sometimes turning to Aken'ova as a mother figure. Earlier in the day he called her and said he wanted to return home. "Just stay where you are until things calm down," she told him gently.

But the longing to be among his friends, including those released from jail, is unbearable. "I just want to be with them. Even if it's just for 30 minutes." Besides, he wants to get information to pass to the lawyer. He will return to the city under cover of nightfall. He will go to meet the parents of one of the jailed men, and help them with bail money. Do I think that's a good idea?

Love can make you do crazy things, I say. "Yes," he agrees despondently.

After a pause, he speaks again. "But if people can learn to hate, do you think they can learn to love?"