The visit to war-torn Mogadishu is the first by a leader from outside Africa in almost 20 years.
The Turkish foreign minister, part of a delegation accompanying Mr Erdogan, told the BBC they wanted to break the idea that the city was a no-go area.
East Africa is suffering from its worst drought in 60 years.
The UN estimates that some 12 million people have been affected.
Somalia, where five districts have been suffering from famine, has been worst hit. Much of the country is controlled by the Islamist al-Shabab group. Al-Shabab, which is linked to al-Qaeda, has banned many aid agencies from its territory.
Foreign visitors to Mogadishu are a rarity, but since al-Shabab recently made what it called a tactical withdrawal from the capital, a few international politicians have come to see for themselves the thousands of famine victims pouring into the city.
'Test for civilization'Mr Erdogan is the most high-profile figure so far to visit Mogadishu, which is now controlled by the weak interim government and by a 9,000-strong African Union force (Amisom).
Correspondents say Turkish flags are flying at the airport, the port and on one of the capital's main thoroughfares.
He travelled through the city in a bullet-proof car, in contrast to the armoured personnel carrier usually used by Somalia's President Sheikh Sharif Ahmed.
Mr Erdogan, accompanied by his wife, daughter and an entourage consisting of cabinet members and their families, said Turkey would open an embassy in Mogadishu to help distribute aid for famine victims.
"The tragedy going on here is a test for civilization and contemporary values," Mr Erdogan told reporters, AFP news agency reports.
His trip comes days after Turkey and other Muslim countries pledged $350m (£212m) for famine relief.
"We came to Somalia to show our solidarity with the brothers and sisters of Somalia, but this is not just for one day, we will continue to work for our brothers and sisters and we will never leave them alone," Foreign Minister Ahmet Davutoglu told the BBC's Focus on Africa programme.
The purpose of the visit was first symbolic, he said.
"There was a perception that nobody can go to Mogadishu; we try to destroy the perception. We came - many others can come."
The second aim was to provide humanitarian assistance and during this Muslim holy month of Ramadan the Turkish public had so far raised $115m for Somalia, he said.
"We are here to make a call to all leaders, to all states that they should do their part as well and not only for short-term humanitarian assistance but the long-term economic development of Somalia."
The visitors observed the distribution of Turkish aid in the internally displaced people's camps.
It was also announced that Turkey would rebuild the road to Mogadishu airport, restore a hospital, build schools and drill water wells.
Disease warningMeanwhile, the UN is warning of an outbreak of cholera among Somali famine victims.
One hospital in Mogadishu has registered more than 4,000 cases.
Continue reading the main story"The situation at the moment is pretty critical," said Nancy Balfour of the UN children's agency, Unicef. "The combination of diarrhoeal disease and malnutrition is absolutely deadly for children."
"Normally, diarrhoea would not kill children, but children in as malnourished a state as we have in Somalia cannot stand the disease, and many, many will die if this outbreak gets out of control," she told the BBC's Network Africa programme.
Ms Balfour said she feared that there were many more cases in the interior of Somalia, which Unicef was having difficulty in reaching.
Before the main cholera season had even started in October, the conditions are already in place for the disease to spread rapidly, she said.
"The conditions are perfect for disease transmission: people are living in crowded conditions, they're using unprotected water sources - usually shallow wells that are open and easily contaminated - and people are very weak from the malnutrition."
More than 100,000 people have arrived in Mogadishu in the last two months in search of food.
The UN says 3.2 million people - almost half the population - are in need of immediate life-saving assistance in Somalia, which has been wracked by civil war for two decades.