China said on Wednesday it won a pledge from North Korean leader Kim Jong Un to denuclearize the Korean peninsula during a meeting with President Xi Jinping, who pledged in return that China would uphold its friendship with its isolated neighbor.

After two days of speculation, China announced on Wednesday that Kim had visited Beijing and met Xi during what the official Xinhua news agency called an unofficial visit from Sunday to Wednesday.

The trip was Kim's first known journey abroad since he assumed power in 2011 and is believed by analysts to serve as preparation for upcoming summits with South Korea and the United States.

Beijing has traditionally been the closest ally of secretive North Korea, but ties have been frayed by North Korea's pursuit of nuclear weapons and China's backing of tough U.N. sanctions in response.

Xinhua cited Kim as telling Xi that the situation on the Korean peninsula is starting to improve because North Korea has taken the initiative to ease tensions and put forward proposals for peace talks.

"It is our consistent stand to be committed to denuclearisation on the peninsula, in accordance with the will of late President Kim Il Sung and late General Secretary Kim Jong Il," Kim Jong Un said, according to Xinhua.

North Korea is willing to talk with the United States and hold a summit between the two countries, he said.

"The issue of denuclearisation of the Korean Peninsula can be resolved, if South Korea and the United States respond to our efforts with goodwill, create an atmosphere of peace and stability while taking progressive and synchronous measures for the realization of peace," Kim said.

Chinese state news outlets had photos of Xi and Kim together:

XHNews tweet

People's Daily tweet.

Xi told Kim in return that both sides had stated repeatedly that their traditional friendship should be passed on and developed better.

"This is a strategic choice and the only right choice both sides have made based on history and reality, the international and regional structure and the general situation of China-North Korea ties. This should not and will not change because of any single event at a particular time," Xi said.

Speculation about a possible visit by Kim to Beijing was rife earlier this week after a train similar to the one used by Kim's father was seen in the Chinese capital, along with heavy security and a large motorcade.

Kim was accompanied by his wife, Ri Sol Ju, Xinhua said.

Xi had accepted an invitation from Kim to visit North Korea, South Korea's Yonhap news agency said.

Improving ties between North Korea and China would be a positive sign before planned summits involving the two Koreas and the United States, a senior South Korean official said on Tuesday.

In response to the Chinese government's announcement of Kim's visit, White House Press Secretary Sarah Huckabee Sanders told reporters, "The Chinese government contacted the White House earlier on Tuesday to brief us on Kim Jong Un's visit to Beijing. The briefing included a personal message from President Xi to President Trump, which has been conveyed to President Trump."

Kim Jong Un's father, Kim Jong Il, met then-president Jiang Zemin in China in 2000 before a summit between the two Koreas in June that year. That visit was seen at the time as reaffirmation of close ties with Beijing.

—CNBC.com contributed to this report.