MOGADISHU, Somalia -- Secretary of State John Kerry made an unannounced trip to Somalia Tuesday in a show of solidarity with a government trying to defeat al-Qaida-allied militants and end decades of war in the African country. He is the first top U.S. diplomat ever to visit Somalia.

Kerry arrived at Mogadishu's airport shortly before noon local time, greeted by Somalia's president and prime minister on the tarmac. He held a series of meetings that included both of them along with regional leaders and civil society groups, before flying back to Kenya a little more than three hours later.

"More than 20 years ago, the United States was forced to pull back from your country," Kerry said in a video address to the Somali people, invoking the "Black Hawk Down" debacle when Somali militiamen shot down two U.S. helicopters. "Now we are returning in collaboration with the international community and bearing high hopes but also mixed with ongoing concerns."

The trip was made under tight security conditions. Somalia's government only found out a day ago that Kerry would join the State Department's top Africa official, Linda Greenfield-Thomas, on the trip. And the fact that he was only dipping his toe in Somalia, and not venturing past the airport, highlighted just how dangerous and unstable the country remains.

"The next time I come, we have to be able to just walk downtown," Kerry told Somali President Hassan Sheikh Mohamud. Downtown, Mohamud replied, "is very different now."

Top of the agenda was the fight against al-Shabab. African forces and U.S. drone strikes have crippled the organization's leadership in recent years and left the extremists without much of the territory they once controlled or the cash flows needed to reverse their losses.

But as al-Shabab has decentralized, the militants in some ways have become even more dangerous, expanding their activities in Kenya and other neighbouring countries. Last month's massacre at Kenya's Garissa University College killed 148 people, mostly students, and underscored the group's capacity to carry out relatively unsophisticated but extremely deadly terrorist attacks far from its bases of operations.

Somalia has been without a truly functioning, nationwide government for two-and-a-half decades. After warlords ousted dictator Siad Barre from power in 1991, they quickly turned on one another and plunged the country into anarchy. Militias, Islamist extremist groups and Somalia's nominally national military all vied for power before the tide turned against al-Shabab earlier this decade. Piracy also has been a major problem.

"Over the past quarter century, you have known immense suffering from violence, from criminals, from sectarian strife, from dire shortages of food, and from an inability to remain safely even within your villages and homes," Kerry said in his address.

"I visited Somalia today because your country is turning around," he said. "Three years have passed since a new provisional constitution was adopted and a parliament sworn in. With help from (African troops), Somali forces have pushed al-Shabab out of major population centres. A determined international effort has put virtually all of Somalia's pirates out of business."

Yet even as a relative calm has settled over parts of the country, including Mogadishu, Somalia remains fraught with a painful history for the United States.

American troops were sent there in 1992 on a peacekeeping mission to help stave off a national famine. They left two years later in humiliation after 18 servicemen were killed in the downing of the helicopters and a subsequent rescue attempt, the indelible memory being the images of American bodies dragged through Somalia's streets.

The Obama administration is banking on Mohamud's government to turn a new page toward democracy and economic development. The U.S. has provided hundreds of millions in military support to build up and professionalize the army, and is working with Mohamud to try to usher in a broader, more representative government over the next 18 months.

If that effort is successful and stability expands, officials say the U.S. could re-establish an American embassy in the capital before President Barack Obama leaves office. For now, the president has nominated a career diplomat, Katherine Dhanani, to serve as the first U.S. ambassador to the country since 1991, with the idea that she would operate out of Nairobi and make regular trips into Somalia.

Britain, Italy and several other countries already have embassies in Mogadishu. Kerry said only the U.S. will start mapping out where a future U.S. diplomatic mission can be built.

Kerry's brief foray comes a day after extensive counterterrorism and refugee talks with Kenya's government, much of it deeply tied to the situation north of the border. For the Kenyans, stability in Somalia can't come soon enough. They are scrambling to combat al-Shabab and even have threatened to begin emptying the sprawling Dadaab refugee camp, the world's largest with some 350,000 Somali inhabitants.

Kerry said he received assurances from President Uhuru Kenyatta that no unilateral action would take place to close Dadaab as the U.S. and others try to make Somalia safe enough to accommodate large-scale refugee returns.

"It's a challenge for Kenya," Kerry said as he spoke to the leaders of Somalia's Puntland, Juba and West Administration regions. "And the solution is for them to be able to come home to peace and safety."

Their discussions came as the region's refugee crisis becomes increasingly complex, with war in nearby Yemen creating conditions so dire that some people are even fleeing to Somalia.

Aid agencies are undertaking contingency planning for a prolonged conflict in Yemen that could prompt 100,000 people to escape across the Gulf of Aden to Somalia and 30,000 to Djibouti this year. It's unclear how Somalia, in particular, would be able to handle such an influx given its persistently high levels of violence.

- Klapper reported from Nairobi, Kenya.