WASHINGTON — The jokes were mostly on Donald J. Trump on Saturday as he spent the evening with a few hundred of his least favorite people — the Washington press corps — at its annual Gridiron Dinner. The president, though, as the guest of honor, got the last laugh, and even thanked the media.

No, really.

Mr. Trump wrapped up his comments by lauding the assembled reporters “for all you do to support and sustain our democracy. I mean that.”

In a more-than-30-minute speech, Mr. Trump cracked jokes about Attorney-General Jeff Sessions, Vice-President Mike Pence — “he is one of the best straight men you are going to meet” — and son-in-law Jared Kushner. He didn’t spare Stephen Bannon, the former White House strategist (“that guy leaked more than the Titanic”) and even mocked his own relentless cable TV-viewing habit.

Mr. Trump in 2017 turned down the traditional invitation extended to the commander in chief by the journalists’ group, and also skipped the White House Correspondents Dinner.

But he flew back Saturday from his club in Palm Beach, Florida, to attend the annual white-tie charity event, a kind of celebrity roast at which some of the country’s best-known print and television journalists perform musical skits that lampoon Washington’s most powerful politicians.

While still in Florida, Mr. Trump stayed true to form, saying on Twitter that the “Mainstream Media in US is being mocked all over the world. They’ve gone CRAZY.” The comment was appended to a tweet from his oldest son, Donald Trump, Jr., highlighting article from a conservative Web site. — Bloomberg