The secret diplomacy that would upend more than 50 years of hostility between the United States and Cuba was a year and half in the making.

It started with an American overture to Cuba and a series of nine meetings in Canada, beginning in June 2013, according to senior administration officials.

The process involved an unusual intervention by Pope Francis, who opened the Vatican to help seal an agreement and wrote personal letters to President Obama and Cuban President Raúl Castro calling for a prisoner exchange and a resumption of diplomatic relations.

For a time, the talks stalled on definitions of who was a spy but ended on an emotional high note as three planes ferried released prisoners between the two countries in a choreographed swap.

The back-channel negotiations were conducted not by professional diplomats but by two of Obama’s national security advisers, making it clear to the Cubans that the opening was coming directly from the White House.

Along the way, administration officials from Secretary of State John F. Kerry on down used every opportunity to reiterate that the future of U.S.-Cuba relations was entwined with that of American contractor Alan Gross, whose physical and emotional stamina seemed to be declining after five years in detention.

In four conversations this summer with Cuban Foreign Minister Bruno Rodríguez Parrilla, Kerry said that relations between the two countries would “never, never” improve so long as Gross was in a Cuban prison, said a senior State Department official, who like others spoke on the condition of anonymity to provide details of the private conversations.

Wednesday’s announcement that Cuba and the United States were moving toward a normal diplomatic relationship was a policy shift that Obama long ago vowed to undertake.

Although he made it easier for Cuban Americans to send small sums of money to relatives on the island and also to go there on visits, the probable fallout from a more dramatic policy change was too daunting in his first term. But Obama believed that continued, permanent estrangement was untenable, officials said. During the 2012 Summit of the Americas, he was on the receiving end of a barrage of complaints from Latin American leaders over what they saw as a U.S. obsession with Cuba.

Early in 2013, as Obama began his second term, he authorized exploratory talks with Cuba.

On both sides, the governments decided to forgo traditional diplomatic channels and place the talks on the level of presidential administration to presidential administration. The White House chose deputy national security adviser Benjamin J. Rhodes, a trusted senior Obama aide, and Ricardo Zúñiga, the senior National Security Council director for Latin America, with previous service at the U.S. interests section in Havana. The Cubans picked comparable aides.

“It was critical that they were identified as speaking on behalf of the respective presidents,” said one official who participated in the talks.

Where U.S.-Cuba relations stand and what may change

The two teams were purposely kept small, the official said.

“We kept it fairly tight on our side, and the Cubans, I think, did the same on their side,” the official said. “From our perspective, we didn’t want any wrench to be thrown in the gears that could complicate attempts to secure Alan Gross’s release.”

In June 2013 came the first face-to-face talks in Ottawa, which has friendly diplomatic relations with Cuba. Over the next 18 months, the teams had multiple meetings in the Canadian capital.

Havana’s opening proposal was for a spy exchange — Gross for the three still-jailed prisoners of the Cuban Five, who had reported to Havana on the activities of anti-Castro Cubans in Miami. The Americans refused, insisting that Gross was a subcontractor with the U.S. Agency for International Development and had been carrying out a legitimate mission.

But soon the talks moved into what it would take to reestablish diplomatic relations.

Then, Obama in March visited Pope Francis at the Vatican, and their talk turned to Cuba. Francis, the first Latin American pope, offered to help resolve difficulties between the two countries and subsequently sent letters to Obama and Castro.

It was well into this year before the Americans suggested that an actual U.S. spy being held by Cuba could be released in exchange for three Cubans imprisoned in the United States.

In October, the two teams traveled to Rome to meet with Vatican officials. The deal for the prisoner exchange was finalized there, although talks continued in Canada up through November.

“The Holy See will continue to assure its support for initiatives which both nations will undertake to strengthen their bilateral relations and promote the wellbeing of their respective citizens,” the Vatican said in a statement congratulating Cuba and the United States on the deal.

Toward the end, events propelled Obama along. After the midterms, he indicated that he felt more able to take unilateral actions using his executive powers.

There also were reports that Gross’s condition was worsening. Gross had told visitors that he would not see the new year behind bars. Although he did not look as frail as some reports indicated, there was concern that he was growing physically weaker and despondent over his long imprisonment.

That worry came to an end this week. On Tuesday, Obama spent close to an hour on the phone with Castro, the first presidential-level conversation involving the two countries since Cuba’s 1959 revolution. And Wednesday, Obama spoke from the White House in a televised address to the American people.

“Change is hard, in our own lives and in the lives of nations, and change is even harder when we carry the heavy weight of history on our shoulders,” said Obama, who noted that he was born two years after Fidel Castro came to power. “But today, we are making these changes because it is the right thing to do.

“Today, America chooses to cut loose the shackles of the past, so as to reach for a better future for the Cuban people, for the American people, for our entire hemisphere and for the world.”

Only a few hours earlier, three planes were cutting through the morning skies.

One left Miami for Havana carrying three Cubans who were convicted in 2011 on spying charges.

Another plane flew in the opposite direction, transporting an unnamed U.S. spy, imprisoned nearly 20 years for providing information on Cuban moles in the United States.

The third had departed from Joint Base Andrews before dawn for a military installation near Havana. There, the head of the U.S. interests section escorted Gross to his waiting wife, Judy.

“For them to be reunited for good, it was a very moving scene,” said Sen. Jeff Flake (R-Ariz.), who visited Gross several times while he was in prison and was on the flight Wednesday.

On the way home, Gross was elated, throwing his hands in the air when the pilot announced they had entered American airspace.

“You’re free,” Sen. Patrick J. Leahy (D-Vt.), who also was on board, said he told Gross.

“I finally know I’m free,” Gross replied.

Adam Goldman contributed to this report.