"When you start treating people differently not because of any harm they are doing to anybody, but because they are different, that's the path whereby freedoms begin to erode," Obama said at a joint press conference with the Kenyan leader in Nairobi. "And bad things happen."
Under Kenyan law, sexual activity between men is illegal and punishable with a maximum imprisonment of 14 years. Many Kenyan leaders had encouraged Obama not to discuss gay rights on his first trip to the country as President.
But Obama equated legalized discrimination of gays to legalized racism in America.
"And when a government gets in a habit of people treating people differently, those habits can spread," Obama continued. "As an African-American, I am painfully aware of what happens when people are treated differently under the law."
Kenyatta, however, said that while the U.S. and Kenya share many common values and goals, gay rights is not one of them.
"The fact of the matter is Kenya and the U.S. share so many values: common love for democracy, entrepreneurship, value for families -- these are some things that we share," Kenyatta said. "But there are some things that we must admit we don't share. Our culture, our societies don't accept."
"It is very difficult for us to be able to impose on people that which they themselves do not accept," Kenyatta continued. "This is why I repeatedly say for Kenyans today the (gay rights issue) is generally a non-issue. We want to focus on other areas."
After that comment, a small amount of applause was heard, presumably from some of the audience attending.
Obama and Kenyatta addressed several other topics as well, including Kenya's economy, their shared fight against terror in the region and Obama's personal connection to Kenya, the country of his father's birth.
Obama highlighted Kenya's improving economy and increased focus on economic development. He announced that the two countries were expanding economic partnerships, including one that would make it easier for businesses to collaborate.
He also said that counterterrorism efforts between the U.S., Kenya and other African countries have greatly reduced the territory controlled by Islamic terrorist group al Shababb.
Obama, who last visited Kenya as a U.S. senator in 2006, apologized for not visiting the nation sooner as President, but said he plans to return after leaving the White House.
"Given the demands of the job and the bubble, I can't come here and just go upcountry and visit for a week and meet everybody," he said. "And that's partly what I had to explain, begging for forgiveness that once I'm a private citizen, I'll have more freedom to reconnect."
The President added that when he returns to Kenya, he wants to work on issues affecting the poor.
"I'm more restricted, ironically, as President of the United States than I will be as a private citizen in terms of some of the hands-on direct help I want to do, partly because of schedule and partly because of making sure that in my relationship to Kenya, I'm understood to be operating as the President of the United States," he said.
Obama and Kenyatta's connection goes beyond their leadership of their respective nations.
Obama's father, Barack Obama Sr., feuded with Kenyatta's father, Kenyan President Jomo Kenyatta, back when the latter was Kenya's president in the mid-1960s. According to Obama's 1995 memoir, "Dreams from My Father," Barack Obama Sr., working as an economist in Kenya's Ministry of Tourism, criticized the Kenyan government's corruption and abuse of power, leading Jomo Kenyatta to fire him.