The changing accounts about the president, the payment and the pornography actress came as a surprise to the attorneys of Ms. Clifford and Mr. Cohen and are forcing some of Mr. Trump’s advisers to prepare for a new round of questions from the public.

As of a few hours before Mr. Giuliani went on television, his revelations were not part of a wider strategy, beyond whatever conversations Mr. Giuliani and Mr. Trump had, two people close to the president’s team said. Some of Mr. Trump’s allies were frustrated that they, once again, had no advance warning of the new narrative, making it more difficult to discuss it adequately as surrogates on television.

The president’s tweets on Thursday had far more formal and legalistic language than his typical morning messages to the world, which often include words in all capital letters and are punctuated with exclamation points. “Money from the campaign, or campaign contributions, played no roll in this transaction,” Mr. Trump wrote. It was not immediately clear whether the tweets were composed by Mr. Trump or if they were written by one of his attorneys or advisers, which happens on occasion.

On Thursday morning, Mr. Giuliani said Mr. Cohen was just doing his job when he made the payment.

“Imagine if that came out on October 15th, 2016, in the middle of the, you know, last debate with Hillary Clinton,” Giuliani told Fox News’s “Fox & Friends.” “Cohen didn’t even ask. Cohen made it go away. He did his job.”

Mr. Cohen had worked for Mr. Trump for a decade and has said he would “take a bullet” for him. Mr. Trump, however, treated Mr. Cohen poorly over the years, people familiar with their relationship have said.

Ms. Clifford is suing Mr. Cohen to try to be released from the nondisclosure agreement. And Mr. Cohen is under federal investigation for bank fraud, raising concerns in the president’s inner circle that Mr. Trump’s longtime personal lawyer will cooperate with the government. Federal agents raided Mr. Cohen’s office and home last month and seized documents that included information about payments to Ms. Clifford.

Mr. Cohen recently invoked his constitutional right to take the Fifth Amendment in the ongoing Stormy Daniels case.