Jim Mattis, the former defense secretary, departed from his usual reticence about President Trump and mocked his former boss in a speech on Thursday night that made fun of his bone spur diagnosis during the Vietnam War and his love for fast food.

The president had recently referred to Mr. Mattis, a retired four-star Marine general, as “the world’s most overrated general.” Mr. Mattis picked up the reference and ran with it for several minutes after being introduced as the keynote speaker at the Alfred E. Smith Memorial Foundation Dinner, an annual charity event in New York where politicians and other leaders typically roast one another.

“I’m not just an overrated general,” Mr. Mattis said. “I’m the greatest, the world’s most overrated,” he said, to laughter and applause.

Turning to address Senator Chuck Schumer, Democrat of New York, Mr. Mattis said he “owed” thanks to New York and to Mr. Schumer for bringing up the general’s name during the “contentious” meeting where Mr. Trump called him “overrated.”