Both candidates shied away from the rancor and character attacks of the days leading up to the face-off.
They both said they would work if elected to toughen sanctions on Iran to stop it acquiring nuclear weapons.
The two candidates outlined sharp foreign policy differences during the second of three televised debates in Nashville, Tennessee, but both agreed that Iran should not be allowed to build an atomic bomb.
The United States and other Western powers suspect Tehran is seeking a nuclear bomb under cover of its civilian nuclear program. Iran, the world's fourth-largest oil producer, says it wants to generate electricity only for civilian use and has resisted international efforts to get it to halt its uranium enrichment work.
"We cannot allow Iran to get a nuclear weapon, it would be a game-changer in the region," Obama said. "Not only would it threaten Israel ... but it would also create the possibility of nuclear weapons falling into the hands of terrorists."
Obama said that if elected on November 4 his administration would push to tighten sanctions on Iran and restrict gasoline imports to the Islamic Republic, which suffers a shortage of refined fuel.
"If we can prevent them from importing the gasoline they need and the refined petroleum products, that starts changing their cost benefits analysis, that starts putting the squeeze on them," the Illinois senator said.
He reiterated that while he was prepared to engage in direct talks with Iran, military options were "not off the table."
McCain raised the specter of a nuclear arms race in the Middle East that would threaten the stability of the region and the security of Israel.
"If Iran acquires nuclear weapons all the other countries will acquire them too. The tensions would be ratcheted up," he said, before adding, "We can never allow a second Holocaust to take place."
The Arizona senator said he also favored working with the United States' allies to toughen sanctions to force the Iranians to "modify their behavior."
The United States, Britain, France, China, Russia and Germany gave Iran a beefed-up offer of political and economic incentives in June, including nuclear reactors, in exchange for suspending its uranium enrichment program.
Iran has shown no sign of compromise, however, vowing to resist US "bullying" to force it to abandon its right to develop peaceful nuclear technology.
'Bomb, bomb Iran'
The crumbling US financial system dominated the face-off, as McCain fought to stem his slippage in the polls. That apparently prompted him to propose a striking $300 billion program for the federal government to buy up bad home mortgages and allow homeowners to keep their houses.
McCain said, "Until we stabilize home values in America, we're never going to start turning around and creating jobs and fixing our economy, and we've got to get some trust and confidence back to America."
McCain said he would direct the federal government to buy mortgages directly from homeowners and mortgage providers. The loans would be replaced with fixed-rate mortgages, ostensibly at a loss to the government.
McCain also said America's troubled economy would require the government to scale back benefits now enjoyed by older Americans, and both men agreed that US government entitlement programs – social security retirement payments and medical insurance for the elderly – had to be overhauled.
Neither candidate offered new proposals on the Iraq war, which McCain supports and Obama has opposed from its inception.
The 72-year-old four-term Arizona senator said Obama would bring US troops home from Iraq in defeat. Obama, a 47-year-old first-term senator from Illinois, said the war was draining the US Treasury of $10 billion a month, money that was needed to put a floor under the country's failing financial system.
In one pointed confrontation on foreign policy, Obama bluntly challenged McCain's steadiness. "This is a guy who sang bomb, bomb, bomb Iran, who called for the annihilation of North Korea – that I don't think is an example of speaking softly."
That came after McCain accused him of foolishly threatening to invade Pakistan and said, "I'm not going to telegraph my punches, which is what Sen. Obama did."
While standing back from angry confrontations that were presaged by the vitriol-laced days immediately before the debate, McCain did quip at one point that trying to pin down Obama's tax plan was like "trying to nail Jell-O (gelatin) to the wall."
Obama shot back, "Sen. McCain, I think the Straight-Talk Express lost a wheel on that one," referring to the name McCain has applied to his campaign bus and jet.
They were generally polite, but the strain of the campaign showed. At one point, McCain referred to Obama as "that one," rather than speaking his name.
The debate was the second of three between the two major party rivals, and the only one to feature a format in which voters seated a few feet away posed questions to the candidates. NBC television's Tom Brokaw, the moderator, screened their questions and also chose others that had been submitted online.
They debated on a stage at Belmont University four weeks before Election Day in a race that has lately favored Obama, both in national polls and in surveys in pivotal battleground states.
Not surprisingly, many of the questions dealt with an economy in trouble.
Obama said the current crisis was the "final verdict on the failed economic policies of the last eight years" that President George W. Bush pursued and were "supported by Sen. McCain."
He contended that Bush, McCain and others had favored deregulation of the financial industry, predicting that would "let markets run wild and prosperity would rain down on all of us. It didn't happen."
Obama also said that American International Group Inc., which was bailed out by the government, should give the Treasury $440,000 to cover the costs of a company retreat at a posh California resort less than a week after the federal intervention. "Those executives should be fired," he said, referring to the participants in the retreat.
McCain's pledge to have the government help individual homeowners avoid foreclosure went beyond the details of the bailout that recently cleared Congress. The legislation allows but does not require Treasury to purchase mortgages directly. Obama has said previously that idea should be studied, and his campaign contended McCain's proposal was not a new one.
McCain's campaign issued a written statement that said the $300 billion cost of his initiative would be paid out of the $700 billion approved late last week.
The debate audience was selected by Gallup, the polling organization, and was split three ways among voters leaning toward McCain, those leaning toward Obama and those undecided.
Turn the page
With the stock market plunging, retirement plans evaporating, tens of thousands of homes in foreclosure and unemployment climbing, McCain and his running mate, Alaska Gov. Sarah Palin, spent the days leading to the debate attacking Obama's character, hoping, as the Arizona senator's campaign said, to "turn the page" on the devastated economic landscape.
But the pages appeared glued shut.
The Dow Jones Industrial average continued its decline, dropping more than 5 percent Tuesday, despite the $700 billion financial bailout package President George W. Bush signed into law Friday. The top congressional budget analyst said pension plans have lost as much as $2 trillion - or 20% overall - in the past 15 months. Federal Reserve chairman Ben Bernanke, the nation's central banker, said: "The outlook for economic growth has worsened."
Bush, whose handling of the economy has proved an enormous drag on fellow Republican McCain, was able to say only that "we're going to come through this."
"Have faith, this economy is going to recover over time," said the president, whose approval rating is near a record low.
Apparently taking to heart US polling that showed Obama a clear favorite on economic issues and with a growing lead overall, McCain's campaign deployed Palin to carry the message that Obama is not the kind of person who should be in the White House.
Over the weekend she said Obama sees America as so imperfect "that he's palling around with terrorists who would target their own country," a reference to 1960s-era radical Bill Ayers.
Ayers helped found the violent Weather Underground group, whose members were blamed for several bombings when Obama was 8 years old. Obama has denounced Ayers' radical views and activities.
Obama and Ayers live near each other in Chicago, and once worked on the same charity board. Ayers hosted a small, meet-the-candidate event for Obama in 1995, at the start of his political career. Multiple news accounts have said they are not close. The Obama campaign called Palin's remarks outrageous and grossly exaggerated.
Palin, however, continued her attacks Tuesday at a campaign stop in Florida.
"This election is about the truthfulness and judgment needed in our next president," Palin told supporters. "John McCain has it, Barack Obama doesn't."
Obama answered the initial attacks quickly. As if expecting the assault, his campaign fired back with a 15-minute Web "documentary" that resurrected McCain's links to a financial scandal two decades ago.
Just months into his Senate career, in the late 1980s, McCain made what he has called "the worst mistake of my life." He participated in two meetings with banking regulators on behalf of Charles Keating, a friend, campaign contributor and savings and loan financier who was later convicted of securities fraud.
The Senate ethics committee investigated five senators' relationships with Keating. It cited McCain for a lesser role than the others, but faulted his "poor judgment."
Reuters and The Associated Press contributed to this report