As he sought to build a consensus among Asian leaders about how best to deal with North Korea, Mr. Trump also held a separate meeting with Prime Minister Shinzo Abe of Japan. The two leaders were tackling “the problem and menace of North Korea,” Mr. Trump said.

The wording on climate change in the communiqué represented a victory for Ms. Merkel, who played a major role in forging compromise language after France raised objections.

In most other respects, though, the summit meeting had to be a bitter disappointment for the chancellor. When the meeting was first planned for Hamburg, Ms. Merkel’s birthplace, she would have reasonably expected Hillary Clinton, a likely political partner, to be the American president, and she had expected the event to be a strong part of her re-election campaign for a fourth term, with voting in September.

But Mr. Trump tends to suck all the media air out of a room, even in Germany, where he is deeply unpopular. This summit meeting was always going to be primarily about Mr. Trump and his first meeting with President Vladimir V. Putin of Russia.

It has also been about efforts by most of the rest of the world to cajole the American president into softening his stances on global trade and the climate, with Ms. Merkel in a secondary role, trying to come up with compromises.

Her standing has also suffered as Germans have been shocked by violent protests by a small bloc of anarchists who saw the G-20 as a perfect platform for their rejection of capitalism and order.

The atmosphere around Hamburg has been that of an armed camp, hardly welcoming, with 20,000 police officers asking for further reinforcements to try to protect the various leaders. So far, 213 police officers have been injured, and 43 people have been arrested and 96 more detained.