This video grab shows Kim Jong Un (L) and his wife Ri Sol Ju (R) waving goodbye as they depart by car following a meeting with China's President Xi Jinping in Beijing this week. — AFP/CCTV
Chinese and North Korean state media said the visit started on Sunday and ended0 Wednesday.
Analysts said Xi likely wanted to see Kim to ensure North Korea does not cut a deal with US President Donald Trump that hurts Chinese interests during a summit expected to be held in May.
Beijing had appeared sidelined by Pyongyang's approaches to Seoul and Washington, but Kim's visit puts China firmly back at the centre of the diplomatic game.
“It shows that at this crucial juncture, Kim and Xi believed that it was time to seize the opportunity to consult,” Bonnie Glaser, a China expert at the Washington-based Center for Strategic and International Studies, told AFP.
“Both likely concluded that further deterioration in relations would be harmful.”
Deng Yuwen, an independent Chinese international relations scholar, said North Korea needed to turn to its old ally ahead of the US summit, as Kim will be sceptical that Trump will guarantee the security of his regime.
“North Korea needs the big brother to protect it at a crucial moment,” Deng said.
Xinhua said Kim expressed his willingness to hold summits with Trump and South Korean President Moon Jae-in.
“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 realisation of peace,” Kim said, according to Xinhua.
South Korea said last month after talks with Kim in Pyongyang that he would consider abandoning his nuclear weapons in exchange for US security guarantees, and flagged a halt to weapons tests while talks were under way.
White House spokeswoman Sarah Sanders said Trump received a personal note from Xi about Kim's visit on Tuesday.
“We see this development as further evidence that our campaign of maximum pressure is creating the appropriate atmosphere for dialogue with North Korea,” Sanders said.
Confirmation of the visit ended 24 hours of speculation about the identity of a mysterious North Korean visitor after Japanese media spotted a green train, similar to the one used by Kim's father, arriving in Beijing on Monday and departing the following day.
During the news blackout, a heavy police presence at key venues, barricades and mysterious motorcades hinted at his presence.
North Korea's official Rodong Sinmun newspaper on Wednesday featured photos of Chinese officials greeting Kim and his wife with flowers at the train station, and his motorcade being escorted by police motorbikes in front of the Forbidden City.
Frayed ties
His visit to China came as a surprise given the state of relations between the Cold War-era allies, which fought together in the 1950-53 Korean War.
China chaired six-party talks on North Korea that collapsed a decade ago, but its calls to revive that forum have not been heeded so far and Beijing appeared to be on the margins when South Korea announced that Kim had offered to meet with Moon and Trump.
Frustrated by its neighbour's nuclear weapons programme and under pressure from Trump, China has used its economic leverage to squeeze Kim's regime.
At the same time, Beijing fears the collapse of the regime in Pyongyang and the instability it would bring, potentially sending waves of refugees into China and the possibility of US troops stationed on its border in a unified Korea.
But Kim's visit appears to have put relations back on track, with Xi inviting Kim and his wife to visit China again.