Pakistani Prime Minister Nawaz Sharif
greeted him at an airport in Lahore during his short layover in the city while en route to New Delhi from Afghanistan. Sharif was accompanied by his brother Shahbaz, Punjab province's chief minister.
Modi met with his Pakistani counterpart at the official residence of the Sharif family in the Punjab town of Raiwind for impromptu talks. The Pakistan visit lasted about two hours.
"Both leaders expressed their desire to carry forward the dialogue process for the larger good of the people of the two countries," said a statement from Pakistan's foreign office.
"It was agreed to continue and enhance bilateral contacts and work together to establish good neighborly relations."
Modi said on Twitter that he "spent a warm evening with Sharif family at their family home," celebrating Sharif's birthday. He said he was "personally touched" by Sharif's gesture of welcoming him at the airport.
The visit occurred after Modi called Sharif to express his desire to make a stop in Pakistan, Pakistani Foreign Secretary Aizaz Ahmad Chaudhry told reporters in Lahore. Sharif happened to be in Lahore at the time.
Chaudhry described the "goodwill visit" as pleasant and said the two leaders needed "to understand each other to open the doors of peace and stability in South Asia."
Modi and Sharif talked about restarting a dialogue and increasing people-to-people contacts between their nations, he said.
The foreign secretaries of both counties are to meet in Islamabad at the end of the month, Chaudhry said.
It was the first time an Indian prime minister has visited Pakistan in almost 12 years.
Relations have been strained between the two countries since 1947, the year the Asian subcontinent was partitioned into Islamic Pakistan and Hindu-majority India as it gained independence from British rule.
The last Indian prime minister to visit Pakistan was Atal Bihari Vajpayee
, when he attended a South Asian summit in 2004 and held talks with then-President Pervez Musharraf
's predecessor, Manmohan Singh, did not visit Pakistan during his two terms that ended in 2014.
Moving forward after difficulties
The visit is a hopeful sign after the off-and-on talks between India and Pakistan, which suffered some setbacks in 2015.
In August, talks were canceled over an agenda conflict
, only to have the national security advisers of both countries meet recently for surprise dialogue in Bangkok, Thailand.
This month, Indian External Affairs Minister Sushma Swaraj also visited the Pakistani capital of Islamabad, signaling a further ease in bilateral ties.
"Prime Minister Modi has once again proved his critics, who call him a Hindu hardliner, wrong by his unconventional style and by leading India's foreign policy from the front," said K.G. Suresh, a senior fellow at the New Delhi-based Vivekananda International Foundation, which was founded by Ajit Doval, India's national security adviser. "The ice has been broken."
Suresh added, "This short visit has raised hopes high for peace and prosperity in South Asia."
Sharif, who attended Modi's inauguration as prime minister last year, invited his Indian counterpart to Pakistan
when they met in Russia in July.
In a joint statement, they expressed Modi's commitment to attend a South Asian summit next year.
Relations between India and Pakistan have been strained over a series of cross-border firing incidents that both countries blamed on each other.
India has also alleged that Pakistani militants were behind terror raids on its soil this year, which Islamabad denied.
A history of conflict
Pakistan and India have fought two of their three wars over the Himalayan region of Kashmir
, an 86,000-square-mile region to the north.
After Britain relinquished control of the area, Kashmir was free to accede to either nation. Maharaja Hari Singh, the ruler of the kingdom at the time, initially chose to remain independent but eventually opted to join India, thereby handing key powers to the central government in New Delhi. In exchange, India guaranteed him military protection and vowed to hold a popular vote on the issue.
Islamabad has always said that majority-Muslim Kashmir should have been a part of Pakistan. A United Nations resolution adopted after the first war called for a referendum allowing the people of Kashmir to choose which country they wanted to join, but that vote for self-determination has never been held.
India has said that Pakistan lends support to separatist groups fighting against government control and that a 1972 agreement mandates a resolution to the Kashmir dispute through bilateral talks.
Efforts to agree on a peace deal took a step back in 2008, when Pakistani militants attacked the Indian city of Mumbai, killing more than 160 people in a series of coordinated assaults.
In 2011, the two countries agreed to return to the negotiating table, and since then, relations have continued to thaw.
In 2012, then-President Asif Ali Zardari
became the first Pakistani leader to visit India in seven years.
He held talks with Prime Minister Singh, who emerged from the 30-minute meeting saying both nations were willing to find "practical and pragmatic" solutions to their differences. Zardari described the talks as fruitful.