In a speech at the Islamic Society of Baltimore on Feb. 3, President Obama said he wanted to highlight the American right to freedom of religion for all, including Muslims. (Reuters)

President Obama Wednesday delivered the comforting sermon to U.S. Muslims that their community leaders have been requesting for years, framing Islam as deeply American and its critics as violating the nation’s cherished value of religious freedom. Obama’s comments came in his first visit as president to a U.S. mosque.

The historic 45-minute speech at a large, suburban Baltimore mosque was attended by some of the country’s most prominent Muslims. In what appeared to be a counter to the rise in Islamophobia, Obama celebrated the long history of Muslim achievement in American life from sports to architecture and described Muslims as Cub Scouts, soldiers and parents, pointing out the mother of the pre-med college student who introduced him at the podium.

“There are voices who are constantly claiming you have to choose between your identities…. Do not believe them…. You fit in here. Right here. You’re right where you belong. You’re part of America, too,” Obama said, his volume rising as he said he was speaking in particular at that moment to young Muslim Americans. “You’re not Muslim or American, you’re Muslim and American. And don’t grow cynical.”

[The fascinating history of Founding Fathers defending Muslim rights]

While Obama has many times, including in the last few months, spoken out against anti-Muslim rhetoric, Wednesday’s visit was the longest and most direct such effort — an intimate conversation between a faith community and a president who has at times seemed to put himself at arm’s length. The atmosphere was one of a pep talk and was interrupted many times by fervent applause.

The speech was one of several almost back-to-back, high-profile Obama addresses to U.S. faith communities, talks he seems to be using to focus on religious tolerance during an election season where faith often comes up in fiery contexts. One week ago he spoke at the Israeli Embassy, saying the impulse to stigmatize people of other faiths is “deep within us.” On Thursday he will address one of the most high-profile evangelical events, the National Prayer Breakfast.

Obama’s comments come at a time when the country is furiously debating what constitutes religious freedom and who has the right to proclaim it: Is it for the Muslim refugee from Syria? The conservative Christian baker or photographer who can’t in good conscience participate in gay weddings? The Orthodox Jewish private schools seeking funding? His comments Thursday will likely be closely watched. Some critics on social media Wednesday portrayed Obama as a hypocrite:

Muslim American leaders have been pushing for years for Obama to visit a mosque because they feel their community has been defined, and stigmatized, since the Sept. 11, 2001, attacks by images of Muslims related to terrorism, including those from the attacks in Paris and San Bernardino and Islamic State militants. The expression experts often use is that Muslims are stuck in front of a “security lens.”

That’s why the visit Wednesday at the large Islamic Society of Baltimore was being watched closely by Muslims eager for a rebranding.

A new poll out Wednesday by the Pew Research Center shows that the overwhelming majority of Americans — 68 percent — see the problem of religious violence as really being about “violent people using religion to justify their actions,” compared with 22 percent who say some religious teachings promote violence. However, for those who said some religions promote violence, Islam was by far the most common religion named, with 14 percent of respondents.

Aside from the fact that presidents don’t often visit houses of worship outside their own church time, the optics of a mosque visit by this president have been been particularly difficult to navigate for a president who is believed to be Muslim by about one-third of Americans, according to some polls. (He’s Christian.) The White House has been talking about this trip since last fall, said spokeswoman Jen Psaki. That was about the time Republican candidates began ramping up comments about Muslims that set off Islamophobic rhetoric.

“We discussed this as an option of something powerful the president could do to speak directly to Muslim Americans,” she told The Washington Post on Tuesday. The rhetoric, she said, “has really impacted him on a personal level in terms of how damaging he feels it is to this entire community.”

Among those outside the mosque Wednesday was Hasiynah Mohammed, who had driven with her husband and four children from Philadelphia in hopes of getting in. Without a ticket, that didn’t happen. “We’re disappointed, but we’re still excited.”

Mohammed noted that Obama had chosen an immigrant-founded mosque and said it would have been better to pick one with a stronger African American presence, to highlight a segment of the Muslim community that has been here for many generations. She also wished the visit had come earlier in his presidency. “It’s a little, no, it’s a LOT late.”

Obama’s visit is likely to be compared with a landmark speech to the Islamic world early in his presidency. At Cairo University, Obama in 2009 called for a “new beginning” between the Islamic world and the United States, noting shared interests on issues such as extremism but also acknowledging mistakes made over centuries by all societies in the name of culture and faith.

Asked why it took seven years to organize a U.S. mosque event, Psaki said that “was a hard question to answer” and was more about logistics than politics. Many political observers of both parties, however, believe Obama was unable to make the visit before because of the intense anger and fear around Islamist extremism. Some pointed out that the visit Wednesday could be politically beneficial to Democrats (from Obama to 2016 candidates) who can contrast Obama’s comments with the GOP on the issue of Islam. GOP candidates in recent months have talked about excluding Muslim migrants from other countries and creating an official preference for Christians.

Among those who attended the pre-speech roundtable were Ibtihaj Muhammad, a member of the U.S. fencing team who will be the first Olympian to compete in a hijab if she makes it into the 2016 games in Rio; Rami Nashashibi, an artist who directs the Inner-City Muslim Action Network and runs a holistic health center; Imam Khalid Latif, chaplain of the Islamic Center at New York University; Khadija Gurnah, who founded a project for young Muslims; and Suzanne Barakat, a San Francisco doctor whose brother and sister-in-law were killed in the 2015 Chapel Hill killings last year.

Asked Tuesday whether the president was intending to encourage Muslims to be more active in helping police catch radicals, White House spokesman Josh Earnest emphasized to reporters that the point of the visit is to bring up other topics. And to show support for Muslim Americans.

“Look, I also don’t want to leave you with the impression that the president’s remarks at the mosque are going to be focused on national security,” Earnest said. “I think the president is quite interested in making sure that we’re affirming the important role that Muslims play in our diverse American society, and certainly affirming their right to worship God in a way that’s consistent with their heritage.”

Obama did speak Wednesday about Islamist violence, saying Muslims need to play a key role in how their faith is presented.

“It is undeniable that a small fraction of Muslims are propogating a perverted version of Islam. This is the truth,” he said.

Federal prosecutors have charged 77 men and women around the country in connection with the Islamic State. So far, 22 have been convicted.  The FBI says that, in a handful of cases, it has disrupted plots targeting U.S. military or law enforcement personnel.

Since the visit was announced Saturday, Muslim Americans have been discussing the purpose of the visit, the location and the different perspectives that the small, extremely diverse Muslim community has on everything from politics to theology. The event brought to the fore day-to-day conversations among today’s Muslim Americans. Those include the estrangement many young Muslims feel from the institution of the mosque, and the issue of women’s roles in mosque leadership and worship. The Islamic Society is progressive in its pursuit but typically conservative in that men and women sit separately, with men in the primary sanctuary space.

Photos of women who participated in the event with the president showed only women whose heads were covered. According to Pew, about 36 percent of Muslim American women wear hijabs when they are in public.

According to The Baltimore Sun, the Islamic Society’s campus on Johnnycake Road in Catonsville houses a mosque, a school and a seminary, as well as a Girl Scout troop and an athletic club. It was founded in 1969 by three doctors and now has about 3,000 congregants, the Sun reported Tuesday. This week, its leaders were rushing to get their facilities ready for the presidential visit.

The community at the Islamic Society of Baltimore is diverse but consists predominantly of immigrants from Pakistan, India and Bangladesh and their families. The society has its roots in the small group that began meeting on the Johns Hopkins campus to pray, discuss scripture and study Arabic.

The Sun also noted that since Saturday there has been a low buzz about a former longtime imam, Mohamed Adam El Sheikh. In 2004, after he left the Islamic Society, he was quoted as saying that suicide bombings might be acceptable in extreme circumstances. He told the Sun on Tuesday that he had spoken out “repeatedly” since that time against religious extremism and terrorism — a view he expressed to the Sun in 1985, when he was an imam there.

David Montgomery in Baltimore contributed to this report.

Want more stories about faith? Follow Acts of Faith on Twitter or sign up for our newsletter.

Obama thought he could unite a religiously divided America. He was wrong.

When Muslims are the targets, religious freedom advocates largely go silent

Britain might deport Muslim women who can’t learn English. As a Muslim, I support that.