In the sketch, in which he appeared next to the show’s host and his doppelgänger, Larry David, Mr. Sanders protests the way Mr. David seeks special treatment over women and children because he is wealthy.

Mr. David, who was putting on an accent that dipped between Irish and Flatbush, told Mr. Sanders that his ideas “Sound like socialism to me.”

The skit followed an earlier, and frankly funnier, sketch called “Bern Your Enthusiasm,” in which Mr. David revisited the familiar plotlines and characters of his show “Curb Your Enthusiasm,” but in the character of Mr. Sanders.

When he returns to campaign headquarters, Mr. David’s Bernie Sanders discovers that he has lost by essentially five votes, the number of people he offended that day.

Mr. Sanders changed his Twitter avatar to Mr. David playing Mr. Sanders.

The real Mr. Sanders returned to the show one more time, standing next to Mr. David as they prepared to throw over to the night’s musical entertainment.

Mr. David asked him how things were going in New Hampshire, and Mr. Sanders said, “O.K.”.

“It’s pretty, pretty, pretty, pretty good,” Mr. Sanders replied, in an attempt at an impression of Mr. David.

Larry David