McNamara died at 5:30 a.m. at his home, his wife Diana told The Associated Press. She said he had been in failing health for some time.
For all his healing efforts, McNamara was fundamentally associated with the Vietnam War, "McNamara's war," the country's most disastrous foreign venture, the only American war to end in abject withdrawal rather than victory.
Known as a policymaker with a fixation for statistical analysis, McNamara was recruited to run the Pentagon by President John F. Kennedy in 1961 from the presidency of the Ford Motor Co. He stayed seven years, longer than anyone since the job's creation in 1947.
His association with Vietnam became intensely personal. Even his son, as a Stanford University student, protested against the war while his father was running it. At Harvard, McNamara once had to flee a student mob through underground utility tunnels. Critics mocked McNamara mercilessly; they made much of the fact that his middle name was "Strange."
After leaving the Pentagon on the verge of a nervous breakdown, McNamara became president of the World Bank and devoted evangelical energies to the belief that improving life in rural communities in developing countries was a more promising path to peace than the buildup of arms and armies.
A private person, McNamara for many years declined to write his memoirs, to lay out his view of the war and his side in his quarrels with his generals. In the early 1990s he began to open up. He told Time magazine in 1991 that he did not think the bombing of North Vietnam -- the biggest bombing campaign in history up to that time -- would work but he went along with it "because we had to try to prove it would not work, number one, and (because) other people thought it would work."
Finally, in 1993, after the Cold War ended, he undertook to write his memoirs because some of the lessons of Vietnam were applicable to the post-Cold War period "odd as though it may seem."
"In Retrospect: The Tragedy and Lessons of Vietnam" appeared in 1995. McNamara disclosed that by 1967 he had deep misgivings about Vietnam -- by then he had lost faith in America's capacity to prevail over a guerrilla insurgency that had driven the French from the same jungled countryside.
Despite those doubts, he had continued to express public confidence that the application of enough American firepower would cause the Communists to make peace. In that period, the number of U.S. casualties -- dead, missing and wounded -- went from 7,466 to over 100,000.
"We of the Kennedy and Johnson administrations acted according to what we thought were the principles and traditions of our country. But we were wrong. We were terribly wrong," McNamara, then 78, told The Associated Press in an interview ahead of the book's release.
The best-selling mea culpa renewed the national debate about the war and prompted bitter criticism against its author. "Where was he when we needed him?" a Boston Globe editorial asked. A New York Times editorial referred to McNamara as offering the war's dead only a "prime-time apology and stale tears, three decades late."
McNamara wrote that he and others had not asked the five most basic questions: "Was it true that the fall of South Vietnam would trigger the fall of all Southeast Asia? Would that constitute a grave threat to the West's security? What kind of war -- conventional or guerrilla -- might develop? Could we win it with U.S. troops fighting alongside the South Vietnamese? Should we not know the answers to all these questions before deciding whether to commit troops?
He discussed similar themes in the 2003 documentary "The Fog of War: Eleven Lessons from the Life of Robert S. McNamara." With the U.S. in the first year of the war in Iraq, it became a popular and timely art-house attraction and won the Oscar for best documentary feature.
The Iraq war, with its similarities to Vietnam, at times brought up McNamara's name, in many cases in comparison with another unpopular defense secretary, Donald H. Rumsfeld. McNamara was among former secretaries of defense and state who met twice with President Bush in 2006 to discuss Iraq war policies.
In the Kennedy administration, McNamara was a key figure in both the disastrous Bay of Pigs invasion of April 1961 and the Cuban missile crisis 18 months later. The crisis was the closest the world came to a nuclear confrontation between the Soviet Union and the United States.
McNamara served as the World Bank president for 12 years. He tripled its loans to developing countries and changed its emphasis from grandiose industrial projects to rural development.
After retiring in 1981, he championed the causes of nuclear disarmament and aid by the richest nation for the world's poorest. He became a global elder statesman.
McNamara's trademarks were his rimless glasses and slicked down hair and his reliance on quantitative analysis to reach conclusions, calmly promulgated in a husky voice.