Shortly after Trump's remarks, the Department of Energy shared Perry's resignation letter as well as his video message directed to Department of Energy employees.

Perry told Trump earlier that day that he intends to leave his post, according to a White House pool reporter, citing two administration sources.

President Donald Trump confirmed Thursday that Energy Secretary Rick Perry will step down at the end of the year.

Perry "couldn't have done a better job," Trump said at a campaign rally in Dallas on Thursday night, "and he's going to be leaving at the end of the year, and probably coming back here."

Perry, formerly the governor of Texas, had largely avoided headlines since joining the Trump administration in 2017. But in recent weeks he became entangled in the Democratic-led impeachment probe into Trump's actions involving Ukraine.

Trump has criticized some of the people caught up in the inquiry – including a whistleblower whose complaint spurred many Democrats to support starting impeachment proceedings. "Who's the whistleblower? We have to know! Is he a spy?" Trump asked the crowd at the rally.

But the president offered only compliments for Perry at the event, calling him "a friend of mine" who was "so tough" during the 2016 campaign but "gracious" following Trump's victory.

"He's done a phenomenal job. And Rick Perry couldn't have done a better job, and he's going to be leaving at the end of the year, and probably coming back here. Whatever he does, it's gonna be successful. But he spent three years with us, and I want to thank you, Rick. What a job you've done."

"He was also a great governor of Texas," Trump added.

Perry was one of three political appointees overseeing the U.S. relationship with the country after acting White House chief of staff Mick Mulvaney transferred that portfolio away from career staff, George Kent, a senior State Department official, told House investigators this week behind closed doors. Rep. Gerry Connolly, D-Va., described Kent's account to reporters.

Trump also told House Republicans that Perry urged Trump to take the July call with Ukrainian President Volodymyr Zelensky that has become the focus of the impeachment inquiry, according to Axios, a news website. Perry's office has said that he wanted the president to take the call to discuss energy-related matters.

House Democrats, investigating whether Trump conditioned military assistance for the country on an investigation of his political rival, hit Perry with a subpoena for documents earlier this month. The subpoena included a demand for a variety of Ukraine-related materials by Friday.

Perry recently denied reports that he was planning to leave his post.

Read Perry's full resignation letter to Trump:

Dear Mr. President, The opportunity to serve in your Cabinet as the Secretary of Energy of the United States has been an extreme honor. The citizens of this country will continue to benefit greatly from the policies and accomplishments of your Administration for years to come. Your leadership has positively changed the course of this country, and there is much to be proud of, especially in the energy sector. Mr. President, for decades, American Presidents have talked about the importance of energy independence. Under your watch, it has finally been achieved. We no longer depend on other nations for our energy supply-nor are we beholden to the geopolitics of other world leaders for our energy security. Instead, we arrive in their capitals espousing the benefits of American energy resources, technology and services. I can attest first hand that the demand for our product is stronger than it has ever been, and under your leadership that will only multiply. Across the world, we are competing like we never have before. Not long ago, America was an importer of energy. Now, the U.S. private sector is leading the world in energy production, exploration and exports. This historic success speaks to your leadership and willingness to go places where other leaders never thought possible. Today, when the world looks for energy, they can now think of America first. The work being done across the Department of Energy and throughout our national labs to develop more abundant, less expensive and cleaner energy is world-class. From reviving our commercial nuclear energy industry to producing historic levels of domestic oil and gas and renewable energy, America has seen a remarkable turnaround in our energy industry. The Department of Energy has also embraced, with success, the national security mission of modernizing our nuclear enterprise to make sure our weapons are safe and work as designed. We have also achieved unprecedented success in the clean-up of our nuclear facilities. Mr. President, the American energy story is being written every day by millions of men and women in the workforce who are innovating and dedicating themselves to achieving greatness. A perfect example of this are the men and women who are part of the DOE mission. They will be the ones who are at the forefront of innovation, such as with artificial intelligence and cyber security. I am proud to have established the Office of Cybersecurity, Energy Security and Emergency Response and the Office of Artificial Intelligence and Technology. These new offices will help to provide the underlying research and building blocks for government and the private sector to work together in tackling challenges and pursuing opportunities. An important part of the American energy story is what we are doing with science and technology to make our air cleaner. Greenhouse gas emissions have fallen dramatically, with remissions from the electric power sector at their lowest levels since 1987. During the same time, energy-related carbon emissions fell at an even greater pace. This was achieved through American innovation Now more than ever, I believe strongly in the mission of the Department of Energy. The people across the enterprise have a sincere commitment to this country. I feel comfortable that the Department is well prepared to continue this mission with new leadership at the helm. It has been a tremendous honor to serve our country in your administration in such a meaningful way. Anita and I will be forever grateful for this opportunity and I look forward to further supporting you and advancing American leadership in significant ways as a private citizen. Please accept this letter as my official notification that I plan to resign at a date later this year. Most respectfully, Rick Perry

-- CNBC's Kayla Tausche contributed to this report.