Michael D. Cohen, a former lawyer for President Trump, was sentenced to three years in prison on Wednesday after denouncing Mr. Trump and explaining that “I felt it was my duty to cover up his dirty deeds.”

Mr. Cohen gave an emotional apology to the court for his involvement in a hush-money scandal that could threaten the Trump presidency — a scheme to buy the silence of two women who said they had affairs with Mr. Trump to protect his chances before the 2016 election. Mr. Cohen said his blind loyalty to Mr. Trump led him to ignore “my own inner voice and my moral compass.”

The sentencing in federal court in Manhattan capped a startling fall for Mr. Cohen, 52, who had once hoped to work by Mr. Trump’s side in the White House but ended up a central figure in the inquiry into payments to an adult-film star and a former Playboy model before the 2016 election.

Judge William H. Pauley III called Mr. Cohen’s crimes a “veritable smorgasbord of fraudulent conduct” and added, “Each of the crimes involved deception and each appears to have been motivated by personal greed and ambition.”