By Daniel Trotta and Lesley Wroughton

HAVANA/WASHINGTON (Reuters) - The United States and Cuba formally agreed on Wednesday to restore diplomatic relations on July 20, setting up a trip to Havana by John Kerry, who would become the first U.S. secretary of state to visit the country in 70 years.

Sealed by an exchange of letters between U.S. President Barack Obama and Cuban President Raul Castro, the deal fulfills a pledge the former Cold War enemies made six months ago. It also attempts to end the recriminations that have predominated ever since Fidel Castro's rebels overthrew the U.S.-backed government of Fulgencio Batista on Jan. 1, 1959.

The letters set a date of July 20 for the re-establishment of relations, and embassies could be opened at that time or later.

Kerry, speaking from Vienna, said he would visit Havana to raise the U.S. flag outside the future U.S. embassy, currently labeled an interests section.

"The progress that we mark today is yet another demonstration that we don’t have to be imprisoned by the past," Obama said from the White House Rose Garden. "When something isn’t working, we can - and will - change."

Obama noted that ties were severed in the year he was born, 1961.

A long battle of subterfuge and outright aggression followed as Cuba closely aligned with the Soviet Union, nearly leading to nuclear war in 1962 over Soviet missiles stationed on the island.

Isolating Cuba had been a foreign policy pillar under 10 U.S. presidents, continuing long after the fall of the Soviet Union in 1991. Washington's allies repeatedly condemned the policy, and Obama declared it had failed to promote democracy or improve the lives of Cubans living in a one-party state.

"It hasn’t worked for 50 years. It shuts America out of Cuba’s future, and it only makes life worse for the Cuban people," Obama said while noting stark differences between the two governments would remain.

Cuba's Communist government controls the media and represses political opponents, a policy it has justified based on U.S. attempts to destabilize the government and Washington's open support of dissidents.

Story continues

Castro, 84, Fidel's younger brother and Cuban president since 2008, welcomed diplomatic ties that he said should reinforce the principles of sovereign equality and non-interference in his country.

"Cuba is encouraged by the reciprocal intention to develop respectful and cooperative relations between our two peoples and governments," Castro wrote in his letter to Obama.

Obama said the agreement would increase U.S. contact with ordinary Cubans, permit a larger staff of diplomats and grant them more freedom of movement across the island.

Since 1977, both countries have operated with interests sections protected by the Swiss government.

MAJOR OBSTACLES AHEAD

With diplomatic relations restored, the United States and Cuba will turn to more difficult bilateral problems likely to take years to resolve.

Cuba's Communist government said in a statement that to have normal overall relations, the United States must rescind its economic embargo and return the naval base at Guantanamo Bay, which it has leased since 1903, as sovereign Cuban territory.

Asked whether he could envision a day when the United States might give up the base, U.S. Defense Secretary Ash Carter told a Pentagon news conference there was "no anticipation and no plan, with respect to the Guantanamo Bay naval station in Cuba."

Obama, a Democrat, has asked the Republican-controlled Congress to lift the 53-year-old embargo, but the conservative leadership in Congress has resisted.

A Cuban government statement said the United States also needed to halt radio and television broadcasts beamed into the country and stop "subversive" programs that the United States says are intended to promote democracy.

While critics including Republican presidential candidates have accused Obama of capitulation, the Cuba deal marks a major achievement for him. Obama has been criticized for foreign policy stumbles, especially in the Middle East.

Public opinion polls show majority support for engaging with Cuba, even among Cuban-Americans who had historically been firm supporters of the embargo.

Following 18 months of secret negotiations brokered by Pope Francis and Canada, Obama and Castro announced in December they planned to reopen embassies and normalize relations. The agreement also included a prisoner swap.

(Reporting by Daniel Trotta in Havana and Jeff Mason, Roberta Ramptonm Matt Spetalnick and Lesley Wroughton in Washington; Editing by Doina Chiacu and Lisa Von Ahn)