On the morning the U.S. Embassy in Tehran was stormed by Iranian students, its top diplomat was visiting the Iranian Foreign Ministry, discussing security and diplomatic immunity over a cup of tea when word reached him that his embassy was under siege.

L. Bruce Laingen went on to reach State Department officials by telephone from the Foreign Ministry office in downtown Tehran, ordering them to destroy confidential documents before it was too late. His staff was soon overrun, bound and blindfolded. He would spend most of the next 444 days in captivity at the Foreign Ministry, isolated from the dozens of other Americans who were primarily held at the embassy.

The crisis over the fate of 52 hostages, which lasted from Nov. 4, 1979, to Jan. 20, 1981, helped to upend Jimmy Carter’s presidency, precipitated the deaths of eight servicemen in a failed rescue mission, and transfixed millions of Americans, who were left to watch in horror as diplomats were ridiculed and paraded in front of news cameras.

Mr. Laingen, who was 96 when he died July 15 at a retirement community in Bethesda, Md., spent months slipping secret messages to his superiors in Washington via a visiting Swiss envoy, relaying insights on the situation in Iran while trying to spur his release and that of his staff.

At home in Bethesda, his wife, Penne, tied a yellow ribbon around the oak tree in their front yard, a symbol of vigilance and remembrance that was duplicated by tens of thousands of families across the country.

Mr. Laingen later fought for compensation for the Foreign Service officers and other hos­tages who worked under him — and was haunted, friends said, by an ordeal that left many of his colleagues physically and mentally battered.

“We had this ethos in the Foreign Service that if you’re in charge, rightly or wrongly you’re responsible,” said John Limbert, a low-ranking political officer who was taken hostage at the embassy and later became deputy assistant secretary of state for Iran.

“He took that very seriously, and unfortunately not everyone does. We often met after we were released, to talk about what happened. It was clear that it bothered him, probably for the rest of his life, that he couldn’t do more for his people, and that the mission he had” — maintaining a close relationship with the Iranian government — “he couldn’t carry out. Nobody could have.”

In a nearly four-decade career, Mr. Laingen held posts in West Germany, Pakistan and Afghanistan, and served for two years as ambassador to Malta before being assigned to Iran in June 1979. Just four months earlier, supporters of Ayatollah Ruhollah Khomeini, a Shiite cleric, had overthrown the shah and briefly occupied the U.S. Embassy, triggering the departure of William H. Sullivan, the last U.S. ambassador to Iran.

Mr. Laingen was initially told he would serve as chargé ­d’affaires (the highest diplomatic position in the absence of an ambassador) for no more than six weeks while the Carter White House decided what kind of diplomatic presence it would maintain in Iran.

Instead, the country became his home for the next year and a half.

Although he was not a fluent Persian speaker, Mr. Laingen had experience in the country, with stints in Tehran and Mashhad in the 1950s. Soon after his arrival, he cautioned the Carter administration against allowing Iran’s deposed former ruler, Shah Mohammad Reza Pahlavi, into the United States for cancer treatment. The timing was too risky, he argued; many Iranians were still uncertain whether the United States had accepted its Islamic revolution.

His warnings proved all too prescient. Two weeks after Carter authorized the shah to enter the country, Iranian students overran the embassy, a 27-acre compound in central Tehran. Sixty-three Americans were taken hostage there. A group of 13 women and African Americans were released after two weeks, and six others evaded capture altogether, escaping from Iran in January 1980 with help from Canadian diplomats and a CIA team — a caper that was popularized in the Hollywood movie “Argo.”

Mr. Laingen, along with a deputy and security officer, spent most of the hostage crisis inside a diplomatic reception room at the Foreign Ministry office, where they were more or less untouched. They were often visited by the Swiss ambassador, Erik Lang, who surreptitiously took scrap-paper messages from Mr. Laingen and wired them to Washington.

Many of the other hostages, kept under guard in Tehran and later dispersed across the country, were beaten and brutalized, forced to confront mock firing squads, play games of Russian roulette and run blindfolded into trees. In a phone interview, Limbert recalled that he and the other hostages did not know where Mr. Laingen was or whether he was still alive.

At the ministry office, Mr. Laingen was increasingly isolated after the unsuccessful April 1980 rescue mission. Iraq invaded Iran that September, and evenings in the capital were filled with the sound of air-raid sirens; loudspeakers at the Foreign Ministry blasted martial music, including John Philip Sousa marches.

In January, Mr. Laingen and his two American colleagues were ordered into vans and taken to a prison in Tehran, where they spent the final months of their stay in solitary confinement while a team of negotiators worked to release the hostages.

They were eventually taken to the airport about midnight, “forced to walk and run a gauntlet of shouting and pushing militants” who were “determined to have their last word of abuse,” Mr. Laingen recalled in a 1993 oral history.

“But there was the ramp, leading up to a plane, one of two Algerian aircraft, and there in that plane assembled 52 wildly happy Americans, embracing each other, moving up and down the aisle, talking, laughing, shouting, unable to sit more than a few moments, a scene almost incredible, except that it was real, very real.”

“There were uproarious cheers as we cleared the runway,” he added, “more when champagne was broken out when we crossed the Turkish border, and then the beginning of a flight to freedom we can never forget.”

Lowell Bruce Laingen was born on a farm in south Minnesota, near the towns of Odin and Butterfield, on Aug. 6, 1922. His studies at St. Olaf College in Northfield, Minn., were interrupted by World War II.

Mr. Laingen served as a Navy supply officer in the Philippines and, after graduating from St. Olaf in 1947, he received a master’s degree in international relations from the University of Minnesota. He joined the Foreign Service in 1950 and remained at the State Department until retiring in 1987.

After returning from Iran, Mr. Laingen visited the White House and met with President Ronald Reagan, whose inauguration coincided with the release of the hostages. He later served as vice president of the National Defense University in Washington; was executive director of the National Commission on the Public Service, which studied government reform; and contemplated a run for U.S. Senate in Maryland. His wife said she talked him out of it, in part because he had been away for too long.

She confirmed his death, saying he had Parkinson’s disease and dementia.

In addition to his wife of 62 years, the former Penelope Babcock of Bethesda, survivors include three sons, all retired Navy officers: Chip Laingen of Woodbury, Minn., James Laingen of Haymarket, Va., and Bill Laingen of St. Louis; a sister; 10 grandchildren; seven great-grandchildren; and two great-great-grandchildren.

In retirement, Mr. Laingen joined lawyer Thomas Lankford in a decades-long battle to win compensation for the surviving hostages and their families. Several attempted suicide, and “14 died early deaths,” Lankford said by phone, “all connected to their physical and mental torture.”

In 2015, Congress passed legislation granting up to $4.44 million to each of the surviving hostages and the families of the deceased. But the hostages have received less than 18 percent of their promised compensation, Lankford said, as most of the fund has gone toward relatives of 9/11 victims.

“Here we are 40 years later, having been promised that we would be compensated, and we’re still waiting. It’s just plain wrong,” Lankford said. “It’s shameful.”

Correction: A previous version of this obituary incorrectly described events at the U.S. Embassy in Tehran. American hostages were paraded in front of news cameras outside the embassy, rather than marched through city streets, and by 1979 the embassy was located near the center of the capital, not on the outskirts. This story has been updated.