While President Obama was meeting Friday with other leaders of the G-20 nations in a magnificently rebuilt and extravagantly decorated palace on the outskirts of the city, a small band of gay activists unfurled a rainbow flag and several banners in the heart of the old capital and attracted a crowd of taunting, stone-throwing opponents.

The counterdemonstrators were blessed by a priest. Police stood by impassively.

“Stop homophobia in Russia,” read one sign, in English, that was captured on a video of the demonstration posted online by Radio Liberty, the U.S.-financed news service.

A particularly loud counterdemonstrator taunted a young man about his friends in America.

In the evening, the U.S. president met with nine Russian civil society leaders to express his solidarity before boarding his plane home. Prominent among them were several LGBT activists who feel they have become targets in a new Kremlin-backed campaign.

Obama’s decision to meet with them was an unmistakable signal directed at Russian President Vladimir Putin.

“The government wants to divert attention from other problems by seeking out an internal enemy, as Hitler did with the Jews in Germany,” Olga Lenkova of the St. Petersburg organization Vykhod, or Coming Out, told a reporter from the Spanish news agency EFE earlier in the day.

She said she knew that Obama’s gesture wouldn’t in itself bring about change in Russia but said it was welcome all the same.

With the strong backing of the Russian Orthodox Church, Russia recently enacted a law that criminalizes any act or statement that might be construed as “homosexual propaganda” aimed at children. Gay activists and their allies say that it essentially prohibits any public action on their part.

It appears to be part of a larger Kremlin project to divide society, casting urbanites, liberals, English speakers, environmentalists and other groups as implicit foes of Russia.

This follows on the heels of a law requiring nonprofit groups to register as “foreign agents” — a term that sounds much more sinister in Russian than in English — if they receive any money from abroad and do any work that might be considered political in nature.

Vykhod was fined more than $15,000 in June for refusing to register as one.

Dmitry Peskov, the Kremlin spokesman, denied at the Group of 20 meeting that there was any crackdown on gays.

“There are no problems with homosexuals,” he said. “Our country has a problem with promoting homosexuality among minors.”

The meeting with Obama was closed to the media and the public.

Before it began, Obama said, “All of these leaders, ranging from business leaders to youth leaders to environmental leaders, those who are advocating on behalf of a free press, the rule of law, all of them contribute in one way or another to continuing to strengthen Russian society and helping to make progress on behalf of all people.

“So the kinds of activities that are represented here are critically important to Russia’s development, and I’m very proud of their work,” Obama said.

At the meeting, Obama said that the U.S. administration has to think carefully before pressing the Russians, said Yana Yakovleva, head of the business advocacy group Business Solidarity.

“His main message was that there’s a lot of differences between the two great powers, and that while deciding which issue to raise, they have to weigh carefully the impact it may have on relations on the whole,” Yakovleva told the Associated Press.

It was Obama’s only stop in Russia outside the G-20 conference. He had already decided not to make time to see Putin in Moscow before it began, though the two men did talk about Syria for about 20 minutes on the summit sidelines.

With Obama during the evening meeting were Susan E. Rice, the national security adviser, and Michael McFaul, the U.S. ambassador to Russia. McFaul has become a lightning rod for criticism in the Russian press because of his active interest in civil society groups.

Besides Lenkova, there was Igor Kochetkov, director of LGBT Network. Also present was Yelena Milashina, a reporter for the beleaguered newspaper Novaya Gazeta and a constant thorn in Putin’s side. She is known as a fearless investigative journalist, and this year the State Department gave her an International Women of Courage Award.

Another participant was Yevgenia Chirikova, who rose to prominence fighting a highway project in Khimki and who later became one of the stars of the 2012 protest movement against Putin and his ruling party.

They were joined by Boris Pustyntsev, head of a 20-year-old group called Citizen Watch, which tries to monitor the behavior of police and other law enforcement agencies.

Pustyntsev attended despite his unhappiness with Obama’s plan to launch a military strike against Syria.

While activists in sunny St. Petersburg talked to Obama about civil society, their brethren in Moscow were standing in the rain to try to do something about it at a rally for opposition mayoral candidate Alexei Navalny.

Though the numbers were modest, their umbrellas filled a downtown avenue. Well-known literary figures and rock musicians spoke to a euphoric crowd. People didn’t expect their candidate to win the election, but they were swooning at the thought that Navalny was running, that he was putting on a vigorous, shake-many-a-hand campaign and that he might force the entrenched establishment Mayor Sergei Sobyanin into a runoff — if he doesn’t go to prison first.

Navalny is running with a five-year sentence over his head. He was convicted of what are widely perceived to be trumped-up corruption charges this summer but then unexpectedly released on appeal, allowing him to run for mayor.

Navalny made an emotional and convincing speech, thanked everyone, and said, “We will win.”

The evening ended with rain and joy as the crowd danced to a band from Belarus once banned by that country's dictator, Alexander Lukashenko.

Natasha Abbakumova in Moscow contributed to this report.