The President called on worshippers at Jamia Mosque where he joined Muslim faithfuls in prayer and in breaking their fast.
His visit was made more significant by the fact that muslims are marking the last five days of the holy month of Ramadan.
The last days of the fasting period are regarded as more rewarding to muslims.
Before the Iftar and the prayers, the President was taken on a tour of Jamia Mosque complex where he visited the library and Horizon TV station operated by the mosque.
He addressed viewers on live TV and extended best wishes to Muslims as they approach the end of the holy month.
Kenyatta later addressed worshippers inside the mosque where he reiterated his rallying call for all Kenyans to be united no matter their background.
“We are proud of our diversity as a nation of many ethnic groups, colours and religions,” said the President.
He urged Muslims and all other Kenyans to always remember to foster peace and unity, adding that tolerance is the bedrock of harmonious coexistence.
The President assured the community that the government will never advocate injustice against any one group of Kenyans.
He said the community should work closely with the government in fighting crime and extremism.
“When we see violence occasioned, this violence is not occasioned by Muslims or Christians but by criminals. It is this criminal that we fight because they are enemies of Muslims and Christians and all other religious groups,” said the President.
Muslim leaders who spoke at the Mosque said the President has opened a new window of unity by making the first visit by a sitting President to a mosque.
Chairman of the Jamia Mosque Committee Sheikh Mohamed Warfa said “this is the first time in the history of Kenya a sitting President has visited a mosque...he is opening new doors for the unity of the country.”
The Treasurer of the Mosque Prof Abdilatif Essajje said Muslims have always stood for peaceful co-existence, fairness and justice for all.
He added that Jamia Mosque has always rallied muslims against extremism.
Earlier in the day, President Kenyatta flagged off foodstuff destined for various mosques.
The food consisting of 480 bags of rice, 340 bags of beans and 190 cartons of cooking oil will be distributed to 13 mosques within Nairobi and will be distributed to needy people.
The President further flagged off a truckload of cattle that will be distributed to the various mosques.