In an apparent criticism of the Bush administration, Mr McCain said the US effort in Afghanistan was "no way to run a war".
Addressing supporters in New Mexico, he said: "If I'm elected president, I will turn around the war in Afghanistan, just as we have turned around the war in Iraq, with a comprehensive strategy for victory."
He also said he would bring al-Qaeda's leader, Osama Bin Laden, to justice.
Mr Obama's foreign policy speech comes ahead of a tour that will include Iraq and Afghanistan, the dates of which have not been disclosed for security reasons.
In his speech at the International Trade Center in Washington, Mr Obama said: "This war diminishes our security, our standing in the world, our military, our economy, and the resources that we need to confront the challenges of the 21st Century."
Al-Qaeda has an expanding base in Pakistan that is probably no farther from their old Afghan sanctuary than a train ride from Washington to Philadelphia
He said the conflict in Iraq must be brought to an end as "the central front in the war on terror is not Iraq, and it never was".
Mr Obama said that as president he would take the US in a new direction, and a priority would be to finish the fight against the Taleban and al-Qaeda, which has an expanding base in Pakistan.
He said the troop surge policy had actually hurt America's overall strategic interests, by diverting resources away from Afghanistan, just as the situation there was deteriorating.
Mr Obama said a withdrawal from Iraq would allow a much-needed redeployment of troops.
He said sustained co-operation was needed between Afghanistan, Pakistan and Nato to root out al-Qaeda and the Taleban.
"It is unacceptable that almost seven years after nearly 3,000 Americans were killed on our soil, the terrorists who attacked us on 9/11 are still at large," he said.
On other issues, Mr Obama made the following pledges:
To use all tools not to allow Iran to develop a nuclear weapon
To invest $150bn over the next 10 years to end America's dependence on foreign oil
To develop new defences to protect against the 21st Century threat of biological weapons and cyber-terrorism
Timetable for withdrawal
BBC diplomatic correspondent Jonathan Marcus says the war over the war in Iraq is moving into high gear.
The essential difference between Mr Obama and his Republican rival, John McCain, is that the Illinois senator wants to set a clear timetable for a withdrawal from Iraq - some 16 months - while Mr McCain insists that the situation on the ground, not timetables, must govern the pace of any withdrawal, our correspondent says.
It is not just a political argument, he adds - it has a huge bearing on the signals that the next US president will send to the Middle East and at root it is a test of their capacity to be Commander-in-Chief.
Opinion polls suggest that Americans remain deeply divided on the best strategy in Iraq, with almost equal support for a clear timetable or for no timetable for a withdrawal.
Mr Obama may not necessarily need to win this argument outright, our correspondent says, but in setting out his foreign policy stall he needs to show that he has credible, concrete positions that make sense of a complex world.
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.