Mr. Kelly told the players that Mr. Trump had intervened on their behalf and that he was “very optimistic that this would be taken care of in short order.”

In China, where the justice system has a very high conviction rate, theft can bring punishment ranging from a few days to years in prison. Mr. Kelly said that had the players been charged with the equivalent of felonies — because of the high cost of the merchandise — they could have received prison sentences of five to 10 years.

“I bet they learned a lesson in their lives,” he said.

Mr. Trump was uncharacteristically quiet about the players and their situation until his overseas trip was winding down. He did not tweet about the case as the players sat trapped in their rooms. American officials did not put out any statements about the situation.

But once he was headed home, Mr. Trump provided the first indications that the actions of the three young men had prompted a conversation at the highest of levels.

“I will tell you, when I heard about it two days ago, I had a great conversation with President Xi,” Mr. Trump told reporters during a brief conversation Tuesday before the students were formally allowed to leave their hotel. “He was terrific, and they’re working on it right now. And hopefully everything is going to work out.”

Mr. Trump called the alleged actions of the basketball players “unfortunate,” and grimly noted the toughness of the Chinese judicial system. “You know, you’re talking about very long prison sentences,” the president told reporters. “They do not play games.”

Mr. Trump has made much of his personal rapport with Mr. Xi, who hosted a lavish state visit last week for the president in Beijing. The two leaders met again at an economic summit meeting on Sunday in Vietnam, where Mr. Trump raised the case of the detained basketball players.