NEW DELHI - A surprise visit to Pakistan on Friday by India's prime minister, Narendra Modi, pressed the reset button on the hot-and-cold relationship between the two nuclear-armed neighbors, paving the way for official dialogue to resume next month.

On his way back from Afghanistan, Modi stopped at Lahore for an unscheduled meeting with Pakistani Prime Minister Nawaz Sharif, after a surprise announcement on Twitter that sent analysts on both sides of the border into a tizzy.

After addressing a joint session of the Afghan parliament early Friday, Modi tweeted good wishes for Sharif on his birthday and said he is "looking forward to meet PM Nawaz Sharif in Lahore today afternoon, where I will drop by on my way back to Delhi."

The two leaders met for a little less than two hours at Sharif's festively lit ancestral home in Lahore, where they talked about improving ties.

"Beyond the noise, a personal connect. The Prime Ministers discuss India Pakistan relations," tweeted Vikas Swarup, a spokesman for India's Ministry of External Affairs. He added that Modi met Sharif at his home as "a special gesture" and blessed the latter's granddaughter ahead of her wedding.

India's NDTV 24x7 news channel called it "Modi's masterstroke." In Pakistan, the phrase "birthday diplomacy" trended.

A statement from Pakistan's Ministry of Foreign Affairs said the leaders "expressed their desire to carry forward the dialogue process for the larger good of the people of the two countries."

Pakistani security analyst Rifaat Hussain said the symbolism of the visit is "huge." He added that the meeting is likely to have been preceded by some "behind the door" preparation.

Modi's colleagues in his Bharatiya Janata Party said, however, that there was no secret back-channel planning.

"It was a spontaneous but bold and innovative decision to visit Pakistan," said Nalin Kohli, a spokesman for the BJP. "The India-Pakistan story has many difficult issues lingering for decades. It is not an easy path ahead. But the two leaders are trying to establish a personal equation that can add momentum to the structured process of official talks in the future."

The two leaders last met during the climate change conference in Paris in November, chatting briefly.

Since independence from British colonial rulers in 1947, the two countries have fought two of three wars over Kashmir, a Himalayan region claimed in its entirety by both but divided between them and administered separately.

The foreign secretaries of both nations are scheduled to meet in January. Masood Ahmad Khan, a former Pakistani diplomat, said Friday's meeting suggests "a political will at the highest level and is, therefore, important, symbolically and substantively."

The last time an Indian prime minister visited Pakistan was in 2004. Sharif, however, came to India last year to attend Modi's swearing-in ceremony.

The chairman of the opposition Pakistan People's Party, Bilawal Bhutto, said in a tweet, "Constant engagement is the only way to resolve all outstanding issues."

Some opposition leaders in India said Modi's diplomacy should move beyond grandstanding and focus on tangible outcomes.

"It is utterly ridiculous. You do not conduct diplomacy at the apex level in such a cavalier manner," said Manish Tewari, a senior leader of the Congress party. "The relationship between India and Pakistan is perhaps the most complex, convoluted and intricate relationship between two nuclear weapon states."