Mayor Michael R. Bloomberg thought Goldman Sachs could use a friend.

The mayor, who earned billions on Wall Street and has a reputation as a staunch defender of corporate culture, dropped by the investment bank’s Manhattan headquarters on Thursday in an unannounced show of solidarity that included handshakes on the trading floor and burgers with the chief executive.

Mr. Bloomberg’s hourlong visit, first disclosed by the mayor’s eponymous news agency, came as Goldman struggled to cope with an onslaught of negative publicity after a former executive’s scathing Op-Ed article in The New York Times accused the firm of wanton greed and excess.

The mayor, who suggested the appearance in a phone call to the bank’s chief executive, Lloyd C. Blankfein, couched his visit as a necessary pick-me-up for a down-on-its-luck city institution.

“It’s my job to stand up and support companies that are here in this city that bring us a tax base and that employ our people,” Mr. Bloomberg said on his weekly radio show on Friday. He castigated the news media for “piling on” the bank, saying “ridiculous isn’t even the right word” to describe the coverage.