President Barack Obama meets with members of Muslim-American community at the Islamic Society of Baltimore, Wednesday, Feb. 3, 2016, in Baltimore, Md. Obama is making his first visit to a U.S. mosque at a time Muslim-Americans say they’re confronting increasing ... more >

In his first visit to a U.S. mosque, President Obama thanked American Muslims for their contributions to the nation, promised they will be treated with sensitivity in counterterrorism probes and said Islam is not to blame for terrorist attacks.

“I want to say two words Muslim Americans don’t hear often enough: Thank you,” Mr. Obama said at the Islamic Society of Baltimore. “We are one American family.”

Saying anti-Muslim rhetoric from Republican presidential candidates will discourage Muslims from cooperating with law enforcement, Mr. Obama offered a blanket pledge that the FBI and other agencies will treat American Muslims with respect in terrorism investigations.

“I want you to know that from the president to the FBI director to everybody in law enforcement, my directive, and their understanding, is that this is something we have to do together,” Mr. Obama said. “Engagement with Muslim-American communities must never be a cover for surveillance. We can’t give in to profiling entire groups of people. There’s no one single profile of terrorists.”

The president criticized Republican presidential candidates such as Donald Trump and Sen. Ted Cruz of Texas, without mentioning them by name, for proposing to restrict Muslims from immigrating to the U.S. from the Middle East.

“We’ve heard inexcusable political rhetoric against Muslim Americans that has no place in our country,” Mr. Obama said. “We have to reject a politics that seeks to manipulate prejudice or bias and targets people because of religion.”

Mr. Obama also ridiculed critics who say he can’t effectively fight extremist violence because he refuses even to utter the phrase “Islamist terrorism.”

“I often hear it said that we need more clarity in this fight, and the suggestion is somehow that if I would simply say these are all Islamic terrorists, then we would actually have solved the problem by now, apparently,” the president said. “I agree we actually do need moral clarity. Groups like [the Islamic State] are desperate for legitimacy. I refuse to give them legitimacy. They’re not defending Islam. And we can’t suggest that Islam itself is at the root of the problem. That betrays our values.”

A poll released Wednesday found that Americans are divided along partisan lines about whether the next president should criticize Islam when speaking about terrorists.

The Pew Research Center survey found that 70 percent of Democrats and independents who lean Democratic say the next president should speak carefully about Islamic extremism so as not to criticize Islam as a whole.

But Republicans prefer “blunt talk,” Pew said. In the poll, 65 percent of Republicans and those who lean toward the Republican Party said Mr. Obama’s successor should speak bluntly about Islamic extremism, even it means being critical of Islam.

Of the Republicans who feel that way, 63 percent think Mr. Trump would be a good or great president and 61 percent said the same about Mr. Cruz.

Mr. Trump and Mr. Cruz proposed to restrict Muslim immigrants in the wake of terrorist attacks late last year in Paris and in San Bernardino, California. White House officials say such comments discourage allies in the Middle East from helping the U.S. fight the Islamic State and that it helps extremist groups recruit young Muslims in the West to carry out attacks.

The effort to reach out to American Muslims also comes at a time when the public’s confidence in Mr. Obama’s ability to prevent terrorist attacks is at an all-time low.

Despite the media attention surrounding Mr. Obama’s speech, he is not the first president to visit a mosque. Republican George W. Bush visited a mosque after the 9/11 attacks.

The president was invited by mosques frequently in the first seven years of his presidency, but he stayed away until now. There has been persistent belief among Americans — 29 percent in a poll last fall — that Mr. Obama, a Protestant Christian, is actually a Muslim.

The Islamic Society of Baltimore has been criticized by some for past ties to extremism. An imam who led the mosque for 15 years until 2003 was a member of the Muslim Brotherhood and said in an interview in 2004 that Palestinian suicide bombings are sometimes justifiable.

Mr. Obama openly sympathized with Muslim Americans in his address, saying, “Your entire community so often is targeted or blamed for the violent acts of the very few.”

He spoke directly to young Muslim Americans at one point, urging them to resist extremist propaganda on social media.

“Do not believe them,” the president said. “You’re right where you belong. You’re part of America, too. Don’t grow cynical. Help move our county forward, your country forward.”

Mr. Obama praised the contributions of Muslim Americans throughout U.S. history, calling them “some of the most resilient and patriotic Americans you’ll ever meet.”

But he also said they must acknowledge that “a small fraction of Muslims propagate a perverted interpretation of Islam.”

“Groups like al Qaeda and ISIL, they’re not the first extremists in history to misuse God’s name,” Mr. Obama said. “We’ve seen it before across faiths. But right now, there is an organized extremist element that draws selectively from Islamic text, twists them in an attempt to justify their killing and their terror. And this warped thinking that has found adherence around the world, including as we saw in Boston, Chattanooga and San Bernardino, is real. And it creates tensions and pressure that disproportionately burden the overwhelming majority of law-abiding Muslim citizens.”

He called on people of all faiths, including Muslim leaders, to speak out when any religious group is persecuted, including Christians in the Middle East.

“If we’re serious about freedom of religion — and I’m speaking now to my fellow Christians who remain the majority in this country — we have to understand attack on one faith is attack on all our faiths,” Mr. Obama said.

Before his speech, Mr. Obama met privately at the mosque with young Muslim Americans, including Sabah Maktar, a student at the University of Maryland’s Baltimore campus, where she is studying biology as part of a pre-med program.

“Some of us may find ourselves doubting where we fit into our society,” she said later. “Personally, this visit by the president is an affirmation … an assurance to Muslim-American kids constantly bombarded by anti-Muslim slogans, that they belong.”

The president said some of the Muslims asked him in the meeting, “Why is there always a burden on us? When a young man in Charleston shoots African-Americans in a church, there’s not an expectation that every white person in America suddenly is explaining that they’re not racists.”

Mr. Obama told them, “I recognize that sometimes that doesn’t feel fair.” But he said Muslim Americans still must be “out there on a consistent basis” to decry extremism.

“This is a struggle between the peace-loving, overwhelming majority of Muslims around the world and a radical, tiny minority,” he said.

Riham Osman, a member of the Muslim Public Affairs Council, a civil rights group, said the most important part of Mr. Obama’s speech was highlighting instances in which Muslims have helped victims of terrorism. She said it was “very powerful.”

Mr. Obama cited “Muslims in Kenya who saved Christians from terrorists,” as well as Muslim Americans who raised money for the families of victims of the San Bernardino attack.

In the Pew survey, 52 percent of Americans said they personally know someone who is Muslim. Pew found that 59 percent of Americans believe Muslims in the U.S. face a lot of discrimination; 74 percent of Democrats feel that way, compared with 42 percent of Republicans.