Robert Carswell, a Treasury Department official who played a crucial role negotiating for the 1981 release of 52 Americans held hostage in Iran, died July 22 at his home in Great Barrington, Massachusetts. He was 87.

He had dementia, said his wife, Mary Carswell.

Carswell, a lawyer, worked for more than 40 years at the New York-based firm of Shearman & Sterling. He also served under three Democratic presidents – John F. Kennedy, Lyndon Johnson and Jimmy Carter – and was known in Washington for his negotiating skills.

After President Carter appointed him deputy treasury secretary in 1977, Carswell helped pull New York City out of an ongoing fiscal crisis, securing $1.65 billion in federal loan guarantees two years after the city had been near bankruptcy. He also aided in engineering a $1.5 billion government bailout – among the largest in U.S. history – that saved the automaker Chrysler from bankruptcy.

He added a diplomatic role to his portfolio after armed Iranian students seized the U.S. Embassy in Tehran on Nov. 4, 1979, taking more than 60 Americans hostage in protest of friendly U.S. treatment of the country’s recently deposed shah.

Following orders from Carter, he devised a series of regulations to freeze several billions of dollars in Iranian gold and bank assets. On travels to Europe and Asia, he had mixed success encouraging other countries to join the United States in placing economic sanctions on Iran.

In April 1980, a U.S. military attempt to rescue the hostages ended in failure. Eight American soldiers died when a helicopter collided with a transport plane over the Iranian desert.

A deal was finally struck after 444 anxious days. The hostages were freed just as Ronald Reagan took office.

Pieced together by Carswell and other State and Treasury department officials – and with the help of Algerian mediators – the $8 billion deal unfroze Iranian assets and created a legal path to resolve claims made against Iran by American banks, companies and individuals.

Carswell told the New York Times that it was “probably the most complex financial transaction in history.”

“And boy,” he added, hours after the hostages left Iranian soil, “am I glad it’s over.”

Robert Carswell was born in Brooklyn on Nov. 25, 1928. His father, William, was an appellate division justice of the New York Supreme Court and dean of Brooklyn Law School.

Carswell earned a bachelor’s degree in government and economics from Harvard in 1949. He graduated from Harvard Law School three years later.

He joined Shearman & Sterling as an associate in 1952 and returned after a few years of Navy service. At the firm, in 1956, he oversaw the initial public offering for the Ford auto company – “a crucial but not very glamorous job,” as he later put it. At the time, it was the largest IPO in U.S. history.

He was named special assistant to Treasury Secretary C. Douglas Dillon in 1962. After the assassination of President Kennedy one year later, he represented the Secret Service before the Warren Commission, which was charged with investigating Kennedy’s death. Carswell later advised the Clinton White House on presidential security.

He returned to Shearman & Sterling in 1965 and from 1985 to 1991 was a senior partner. He retired in 2014.

Carswell served on the boards of groups such as the Carnegie Endowment for International Peace, the New York City Bar Association and the Private Export Funding.

His wife of 59 years, the former Mary Wilde, was executive director of the MacDowell Colony, an artists’ haven in New Hampshire. Additional survivors include two children, Kate Carswell of Santa Fe, New Mexico, and Will Carswell of Hadley, Massachusetts; and two grandsons.