Secretary of State John Kerry made a brief but symbolically significant stopover in Somalia on Tuesday, becoming the highest-ranking American official to visit the war-ravaged country since a disastrous foray by American forces more than 20 years ago.

Mr. Kerry did not leave the heavily defended airport in Mogadishu, the capital, which has been chronically hit by bombings and other guerrilla attacks by the Shabab, the militant extremist group that once ruled large parts of Somalia and now has a resilient, albeit smaller, presence in the country.

He conferred with President Hassan Sheikh Mohamud and Prime Minister Omar Abdirashid Ali Sharmarke at the airport, according to reporters traveling with Mr. Kerry. He said that Somalia has been moving beyond the mayhem, despair and warlordism that once made it one of Africa’s most dysfunctional countries.

“I visited Somalia today because your country is turning around,” Mr. Kerry told his hosts.

In a video message to the Somali population, Mr. Kerry said: “We all have a stake in your success. The world cannot afford to have places on the map that are essentially ungoverned.”