“We have a tremendous trade imbalance, but we’re going to get that straightened out,” he said before meeting Ms. Merkel on Saturday morning. “We all understand each other.”

American and Chinese officials conducted quiet talks about a compromise over the last several weeks. But the outcome remained in doubt until the end, when Mr. Trump, flanked by his top aides, sat down with Mr. Xi and his aides to a meal of grilled sirloin and bottles of Malbec.

Mr. Trump had veered from optimism to wariness about a deal, sometimes in the course of a single statement. His economic team offered sharply conflicting advice, with moderates like Treasury Secretary Steven Mnuchin counseling compromise, while hard-liners like Peter Navarro, the White House director of trade, urged him to double down on his pressure.

The moderates tried to exclude Mr. Navarro from the trip. But at the dinner in a Buenos Aires hotel, he was there at the table, seated between the national security adviser, John R. Bolton, and the chief of staff, John Kelly. He leaned forward to listen to the president as he urged Mr. Xi to crack down on illicit shipments of the deadly opioid fentanyl.

The White House said that Mr. Xi, in a “humanitarian gesture,” had agreed to designate fentanyl as a controlled substance.

China was quick to portray the agreement as a concession by Mr. Trump. Wang Shouwen, the vice commerce minister, said the president had agreed to revoke his plan to raise tariffs on certain Chinese goods to 25 percent, according to Reuters.

The Chinese foreign minister, Wang Yi, said, “China is willing to expand imports according to the needs of the domestic market and the people,” which he said would “gradually ease the problem of trade imbalance.”