If elected, Mr Obama said he would take "no options off the table" in dealing with the Islamic republic.
Iran insists its nuclear aims are peaceful. On Wednesday President Mahmoud Ahmadinejad said Tehran would not "retreat one iota" in its nuclear activities.
Mr Obama was speaking in the Israeli town of Sderot - the target of frequent Palestinian rocket attacks from the Hamas-controlled Gaza Strip.
Mr Obama reiterated his view that Jerusalem should be Israel's capital, but insisted the city's final status must be decided through peace talks.
He angered the Palestinian leadership last month by saying the city - which Palestinians want as the capital of a future state - should be Israel's "undivided" capital.
Also on Wednesday, Mr Obama held talks with Palestinian leader Mahmoud Abbas in the West Bank town of Ramallah, telling him that he would quickly engage in the Middle East peace process were he elected president.
"Obama confirmed to President Abbas that he will be a constructive partner in the peace process," Palestinian spokesman Saeb Erekat told reporters.
Barack Obama lays holocaust wreath
Mr Obama told Israeli President Shimon Peres he wanted to "reaffirm the special relationship between Israel and the United States, [and] my abiding commitment to its security".
He said it was "my hope that I can serve as an effective partner, whether as a... senator or as a president, in bringing about a more lasting peace in the region."
Mr Obama also met Defence Minister Ehud Barak, opposition leader Benjamin Netanyahu and Foreign Minister Tzipi Livni.
At the Yad Vashem Holocaust memorial museum, the Illinois senator wore a Jewish skullcap as he laid a wreath.
"Let our children come here and know this history so that they can add their voices to proclaim 'never again'," he wrote in the museum's visitors' book.
Mr Obama is in the region to reassure American voters, especially Jewish Democrats, of his foreign policy credentials ahead of November's presidential election, says the BBC's Middle East correspondent Paul Wood.
Mr Obama said the ties between the US and Israel were unbreakable
Our correspondent also says he faces a US electorate which includes 10% who think he is a Muslim, according to a recent opinion poll.
Some also believe he was educated in a madrassa (Islamic religious school) and refused to place his hand on the Bible when sworn into the Senate - all false claims, adds our correspondent.
Mr Obama arrived in Israel on Tuesday night from neighbouring Jordan, where he met King Abdullah.
He earlier joined a US congressional delegation on a visit to Iraq, where he met Sunni tribal leaders in Anbar province and Prime Minister Nouri Maliki in Baghdad.
He repeated his goal of withdrawing US combat troops from Iraq within 16 months should he become president.
He is due to leave for Germany early on Thursday.
Back in the US, Mr McCain said Mr Obama had been wrong to press for withdrawal timetables.
Are you in Israel or the Palestinian territories? Are you following Obama's world tour?
Send your comments using the post form below, or text 61124.
The BBC may edit your comments and not all emails will be published. Your comments may be published on any BBC media worldwide.
This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.