With only the barest of notice or declared interest, Mayor Bill de Blasio suddenly jetted off to Germany on Thursday to join protest events around the Group of 20 meeting in Hamburg, where President Trump is meeting with President Vladimir V, Putin of Russia and other world leaders.

The trip seemed to be an international extension of Mr. de Blasio’s election-year effort to portray himself as a foil to Mr. Trump, and to project his image as a progressive leader.

Mr. de Blasio kept his travel plans secret until shortly before his departure, but a spokesman, Eric F. Phillips, said that he would attend a demonstration on Saturday called Hamburg Shows Attitude. Mr. Phillips said that the mayor would return to New York on Sunday.

Organizers later announced on Twitter that Mr. de Blasio would be the event’s keynote speaker. The event had previously listed as the featured speaker Gesine Schwan, a German politician who has run for president as the Social Democratic Party candidate.