Former President Donald J. Trump returned to X, the social media platform formerly known as Twitter, on Thursday night after a hiatus of more than two years.

Mr. Trump posted a link to his website and a photo of his mug shot in his first new post on X since Twitter banned his account after the U.S. Capitol riot on Jan. 6, 2021. “Never surrender!” the caption under the mug shot read.

Mr. Trump returned to X one day after the airing of an interview he gave to Tucker Carlson, the former Fox News host, who now streams his talk shows on the service. The interview aired as other Republican candidates for president took part in their first debate on Wednesday night.

When Twitter barred Mr. Trump, the company said he had glorified violence with his posts on the platform, encouraging his supporters to storm the Capitol. But Elon Musk, a self-described “free speech absolutist” who bought the social network for $44 billion in October and recently renamed it, said barring Mr. Trump had been a mistake. He reinstated Mr. Trump’s account in November, although it had sat dormant since then.

Mr. Trump relied on Twitter as a megaphone for years, using the platform to attack opponents, rally supporters and issue policy directives. After the ban, he helped start his own social network, Truth Social.

Mr. Trump has a financial stake in Truth Social and is obligated to make his posts available exclusively on the service for six hours before sharing them on other sites, according to a filing with the Securities and Exchange Commission. He can post to any site immediately if the messages involve political messaging, fund-raising or get-out-the-vote initiatives. Mr. Trump has a large Twitter following — 86.5 million people, compared with only 6.5 million who follow his Truth Social account.

After Mr. Musk purchased the company, Mr. Trump said he would not return to Twitter, adding that he preferred Truth Social. He also mocked Mr. Musk last year when the Tesla chief executive threatened to back out of the Twitter acquisition, saying in a Truth Social post that Mr. Musk was overpaying for the platform and had begged him for help on “his many subsidized projects, whether it’s electric cars that don’t drive long enough, driverless cars that crash, or rocketships to nowhere.”