“We could do a deal — you’ll let me know — why it is all the women say no?” she sang, stretching out her arms in a Trumpian gesture. Later she strolled the stage, gesticulating to the audience in Mr. Trump’s signature make-America-great-again style.

The song, traditionally a duet for men, offers advice for picking up women — in this case, female voters. Some of the original lyrics were altered, but some could stand as is, for Mr. Trump’s combative attitude: “If she says your behavior is heinous, kick her right in the Coriolanus!” The crowd, which included Michael R. Bloomberg, the former mayor: the United States ambassador to the United Nations, Samantha Power; Lin-Manuel Miranda; and Bette Midler, loved it.

Her performance came as something of a surprise to the event organizers, who knew only that Ms. Streep, a supporter of Mrs. Clinton’s, wanted to take on Mr. Trump.

“Utterly her idea, beginning to end,” Oskar Eustis, the Public’s artistic director, said after the show. “There were skeptics, there were doubters, but one of those skeptics was not Meryl Streep. She was absolutely sure she could do it. None of us had seen her in costume or makeup, till she walked out tonight.”

Ms. Streep skipped the dinner before the show to get into character, and spent time holed up in her dressing room. “She was showing us this thing that Donald Trump always does,” said the actress Kate Burton, who shared the dressing room with Ms. Streep along with Ms. Baranski, Lily Rabe and Phylicia Rashad. “He apparently does this thing, where he goes to close to his jacket but it doesn’t close all the way, and so he kind of goes for it and then he tries to close it again.”