NAIROBI, Kenya — Wendy Sherman, the under secretary of state for political affairs, visited Somalia on Sunday, becoming the highest-ranking U.S. official to set foot in the country in more than 20 years, the State Department announced Monday.

Sherman, the No. 3 official in the State Department, met with Somalia’s new president, who was selected by his peers in September, and other political and business leaders during her visit to Mogadishu, Somalia’s bullet-riddled seaside capital.

“Under Secretary Sherman stressed her conviction that Somalia is now a place of hope, not of despair,” the State Department said in a statement.

In the past several months, Mogadishu has made a notable turnaround, making such a high-level visit possible. A city that once echoed with gunfire and was haunted by emaciated, hollow-eyed famine victims trudging through its streets is now returning to life with new hotels, new cafés and even a dry cleaner.

Of course, the troubles are far from over and suicide bombers routinely strike, including an attack just a few days ago on a new restaurant. Still, Somalia is in far better shape than it has been since the last functioning central government collapsed in 1991, and Western officials say there is a narrow opportunity right now to make sure the country continues this momentum and does not slide back into war.

“Under Secretary Sherman urged the Somali leadership to continue to consolidate gains by helping local governance structures emerge through community dialogue and reconciliation,” the State Department said.

One big question is what will happen with the thousands of Ugandan peacekeepers in Mogadishu whose steady pummeling of Islamist militants has paved the way for this renaissance. Some Ugandan officials recently threatened to pull their troops out of Somalia because they were angry that a U.N. investigative panel accused Uganda of backing a brutal rebel group in the Democratic Republic of Congo, which the Ugandans vehemently deny.

On Monday, Sherman implied that the Ugandans would stay in Somalia, at least for the time being.

“Uganda will continue to play the leadership role it has, both diplomatically and in terms of military security,” Sherman told reporters in Nairobi, according to Reuters.

The United States has contributed millions of dollars to the peacemaking efforts in Somalia; some U.S. officials still see Somalia as a counterterrorism priority because of Somalia’s recent history as a home to violent jihadists from around the world.