Attorney General Jeff Sessions attended even in the wake of a nasty attack last week by Mr. Trump, who traces many of his woes to Mr. Sessions’s decision to step aside from the Russia inquiry. (Mr. Trump joked that he had offered Mr. Sessions a ride to the event, but that he had “recused himself.”)
And Jared Kushner, the president’s son-in-law and senior adviser, was there with his wife, Ivanka Trump, just days after his security clearance was downgraded amid concerns about his F.B.I. background check.
“We were late tonight because Jared could not get through security,” Mr. Trump joked.
The dinner features speeches, skits and songs performed by the club’s members and invited political guests. In his opening remarks, David Lightman, the president of the club and a national political reporter for McClatchy, motioned to Mr. Trump, saying, “For the first time, he is eating at a hotel that he doesn’t own.”
One skit featured the characters of Vice President Mike Pence and Hope Hicks, Mr. Trump’s closest aide, who is departing. “Chaos is his genius,” the Hicks character says.
But the highlight of the night always comes when the journalists offer the stage to the president for some self-deprecating jokes and good-natured roasting.
For Mr. Trump, who has spent a lifetime flouting political correctness, his participation in the dinner was striking because the club is the Washington embodiment of political correctness. Its credo is that the roasts at the dinner should “singe but never burn.”
This year, Mr. Trump leaned into the flame.
He took aim at his favorite target: “Fake News CNN.” He called Fox News the “fourth branch of government.” And he made fun of Arthur Ochs Sulzberger Jr., who stepped down in December as publisher of The New York Times, saying: “I inherited millions. Arthur inherited billions of dollars, and he turned it into millions.”
The president also poked fun at Stephen K. Bannon, his former chief strategist, who many inside the White House believe was the source for some of the nastiest parts of the recent book by Michael Wolff.
Mr. Bannon, the president said, “leaked more than the Titanic.”
And he directed a jab at himself over his infatuation with the size of his inauguration crowd, claiming credit for all the crowds at the recently completed Winter Olympics in South Korea.
The relationship between any president and the press is fraught. But it is especially true for Mr. Trump, who spent his first year in the Oval Office in a near constant state of agitation about what he views as a media that is unfair and plotting against him.
It wasn’t always so. As a real estate developer and later a reality TV star, Mr. Trump eagerly sought the media’s attention, basking in the fame and celebrity that produced front page articles and frequent appearances on news programs.
As president, he remains drawn to the TV lights and the newspaper headlines, often conducting discussions and negotiations live on television. But he also complains bitterly when the stories turn negative or personal.
During his remarks on Saturday, he acknowledged one of those story lines: that his administration is in constant chaos.
“I like turnover. I like chaos,” Mr. Trump said. “It really is good. Who is going to be the next to leave? Steve Miller or Melania?”
At that point, he looked out to his wife and asked whether she still loved him.
The Gridiron dinner has long been a bipartisan venue for good-natured ribbing at times when tensions between the media, lawmakers and the president are at their highest.
In 2011, President Barack Obama took a few swipes at the conservative media, saying that he enjoyed doing an interview during the Super Bowl with Bill O’Reilly of Fox News. “I don’t often get a chance to be in a room with an ego that’s bigger than mine,” he joked.
Four years later, Mr. Obama tried a bit of humor at his own expense, noting that “if I did not love America, I wouldn’t have moved here from Kenya.”
But Mr. Trump has not always been a good sport when he is on the receiving end of the roasting. In 2011, when Mr. Obama savaged him at the White House Correspondent’s Dinner, Mr. Trump appeared to take it badly, and some say his decision to run for president was a result in part of his anger at Mr. Obama for the jokes.
This time, he turned the tables. Saturday’s dinner was a chance for Mr. Trump to enjoy a bit of humor at other people’s expense. After a song mocking Hillary Clinton that was performed by journalists and Democratic lawmakers, Mr. Trump applauded vigorously.
The president’s appearance at the Gridiron dinner suggests that he may also be planning to attend other events with the media that he has skipped since becoming president.
Early last year, as the president and his aides repeatedly attacked the White House press corps, Mr. Trump announced that he would not attend the White House Correspondents Association’s annual dinner — the first time a president had skipped the event in decades.
The president has also not yet attended the Alfalfa Club dinner, an exclusive black-tie event that often extends an invitation to the president to attend. Club members are politicians, military leaders and business executives who gather each year for a banquet at the Capital Hilton in Washington.
When Mr. Trump skipped the dinner in 2017, he became the first president to miss the party in his first year in office since 1993. Mr. Pence attended that year, along with Ms. Trump, the president’s daughter, and Mr. Kushner, her husband.
Toward the end of his remarks on Saturday, Mr. Trump apologized that he had to be “up early tomorrow morning” to watch “Fox and Friends.”
“This might be the most fun,” he added, “since watching your faces on election night.”