Barack Obama called for equal treatment of gay and lesbian people under the law on his first visit to Kenya since becoming president.
When asked about gay rights during a joint press conference with Kenyan president Uhuru Kenyatta, Obama said that he was "painfully aware of the history when people are treated differently under the law".
He said: "That's the path whereby freedoms begin to erode and bad things happen.
"When a government gets in the habit of treating people differently, those habits can spread."
He had been warned not to bring up the issue on his visit.
Gay sex is a crime in Kenya, punishable by up to 14 years in prison.
Kenyatta said that there were some things that cultures simply did not agree on, adding that gay rights "is not really an issue on the foremost mind of Kenyans. And that is a fact."
Obama heralded Africa as a continent "on the move" Saturday as he opened a U.S.-sponsored business summit in Kenya, the East African nation where he has deep family ties.
"Africa is one of the fastest growing regions of the world," Obama said. "People are being lifted out of poverty."
Obama's visit to Kenya - the first by a sitting U.S. president - has been highly anticipated in a nation that views him as a local son. The US president's late father was born in Kenya and many family members still live here, including his elderly step-grandmother.
"This is personal for me," Obama said. "There's a reason why my name is Barack Hussein Obama."
Much of the president's visit is focused on boosting business and security ties with Kenya, a growing economy grappling with the threat of terrorism, most notably from the Somalia-based al-Shabab network. Nearly two dozen U.S. lawmakers and 200 American investors have joined Obama on his trip, which also includes a stop in Ethiopia.
At the Global Entrepreneurship Summit on Saturday, Obama announced more than $1 billion in new commitments from the U.S. government, as well as American banks, foundations and philanthropists. Half of the money will go to support women and young people, who Obama says face bigger obstacles when trying to start businesses.
"If half of your team is not playing, you've got a problem," Obama said, referring to women excluded from the formal economy.
Obama hosted the inaugural entrepreneurship summit at the White House in 2010. This year's conference is the first to be held in sub-Saharan Africa.
President Kenyatta, who co-hosted the summit with Obama, lamented that the continent's security and other challenges, including the 2013 attack on an upscale Nairobi mall, had created a negative reputation. He said he hoped Obama's visit would help change the narrative about Kenya and Africa.
"Africa is the world's newest and most promising frontier of limitless opportunity," Kenyatta said. "Gone are the days when the only lens to view our continent was one of despair and indignity."
At the two leaders sat down for a formal meeting at Kenya's State House later on Saturday, Obama emphasised the need for timetables and concrete plans to make progress for the region. He said the U.S. wants to partner with Africa "not out of charity, but because we see opportunity."
"What happens in Africa is going to affect the world," Obama said.
While in Nairobi, Obama toured an innovation fair highlighting the work of vendors working with his Power Africa initiative, which aims to double sub-Saharan access to electricity. As he perused solar panels and posed for photos, Obama acknowledged concerns that the program's progress has been slow, but said it would soon help millions and that building power plants takes time even in the U.S.
Obama also placed a wreath at the site of the 1998 bombing of the U.S. Embassy in Nairobi. The president bowed his head for a moment, then studied the names of the victims etched into a brick wall.
Extremists simultaneously attacked the U.S. embassies in Nairobi and Dar es Salaam, Tanzania, on Aug. 7, 1998. The Kenya attack killed more than 200 Kenyans and 12 Americans at the embassy. Thousands were injured.
Obama arrived in Kenya late Friday and spent the night reuniting with his father's family. Security was tight in the Kenyan capital, with some of the city's normally bustling streets closed to traffic and pedestrians during his visit.
There was palpable excitement in Nairobi for Obama's long-awaited visit. U.S. and Kenyan flags lined the main road from the airport and billboards bearing Obama's picture dotted the city. Local newspapers marvelled at the massive U.S. Secret Service contingent that accompanies Obama whenever he travels overseas.