Obama's visit to the mosque was aimed at correcting what he said was a "hugely distorted impression" of Muslim-Americans, who have come under heavy criticism during this presidential-election cycle. GOP candidate Donald Trump has called for temporarily barring any Muslims, including U.S. citizens, from entering America, and other candidates have warned of "radical Islamic terrorism."
"We've seen children bullied. We've seen mosques vandalized," Obama said. "That's not who we are."
He said that there have been times when America has fallen short of its ideals, including the oppression faced by Mormons over time and the criticism Kennedy took as a Catholic while running for president.
Members of The Church of Jesus Christ of Latter-day Saints were driven from several communities as the faith grew in the 1830s and 1840s, eventually pushing the adherents to the remote Salt Lake Valley. The first Mormon prophet, Joseph Smith, was killed by a mob in Illinois, and Missouri Gov. Lilburn Boggs issued an executive order saying that Mormons were "enemies" and "must be exterminated or driven" from the state.
Obama said that America's founders knew religious liberty was important to strengthen the country, and he reminded religious followers that "an attack on one faith is an attack on all our faiths."
"When any religious group is targeted, we all have a responsibility to speak up," the president said. "And we have to reject politics that seeks to manipulate prejudice or bias and targets people because of religion."
Obama was pointed in acknowledging that concerns about violence emanating from some corners of the Islamic world were not unfounded. He denounced what he called an "organized extremist element" twisting selective Islamic texts in a way that ends up reflecting negatively on the overwhelming majority of law-abiding Muslims.
"It is undeniable that a small fraction of Muslims propagate a perverted interpretation of Islam. This is the truth," Obama said, adding, "It's real. It's there."
But he said suggestions that Islam is at the root of the problem only play into terrorist propaganda, weakening U.S. national security as opposed to strengthening it. He said extremist groups are desperately working to legitimize themselves by masquerading as religious leaders and holy warriors.
"We must never give them that legitimacy. They're not defending Islam," Obama said. "The vast majority of the people they kill are innocent Muslim men, women and children."
Utah Islamic leaders have condemned recent terrorist attacks in Paris, Beirut and California, emphasizing that the assailants violated Muslim scripture.
Ahead of his speech at the suburban Islamic Society of Baltimore, Obama met with Muslim university chaplains, community activists and public-health professionals to discuss religious tolerance and freedom. Among the participants was fencer Ibtihaj Muhammad. The White House said she'll make history at the Rio de Janeiro 2016 Olympic Games as the first U.S. Olympian to compete in a hijab.
Nearly half of Americans believe at least some U.S. Muslims are anti-American, according to a new Pew Research Center poll released Wednesday.
Two-thirds of Americans said people, not religious teachings, are to blame when violence is committed in the name of faith. However, when respondents were asked which religion they consider troubling, Islam was the most common answer.
"We never thought that when we held our first prayers in the small room nearly a half a century ago that we would be hosting the president," said Muhammad Jameel, the mosque's president. "Today is a new starting point. It is also a continuing journey a journey steeped in American history and tradition."
The Associated Press contributed to this report.