"The world is changing," Obama said in a message he said was for "friend and foe alike" but which seemed directed to remaining autocratic leaders across the region. "You have a young, vibrant generation within the Middle East that is looking for greater opportunity, and that if you are governing these countries, you've got to get out ahead of change, you can't be behind the curve."
Obama was asked at a White House news conference about the mood of change sweeping the Middle East in sympathy with the opposition victory in Egypt.
"I find it ironic that you've got the Iranian regime pretending to celebrate what happened in Egypt, when in fact they have acted in direct contrast to what happened in Egypt by gunning down and beating people who were trying to express themselves peacefully in Iran," he said.
Obama said that with advances in freedom of communication through smart phones and Twitter, it is more true than ever that governments must recognize that they must act with the consent of the people.
"Governments in that region are starting to understand this," Obama said, "and my hope is that they can operate in a way that is responsive to this hunger for change, but always do so in a way that doesn't lead to violence."
Iranian leaders had sought to portray the toppling of Zine El Abidine Ben Ali in Tunisia and Hosni Mubarak in Egypt, both secular leaders, as Islamic uprisings.
Hardline Iranian lawmakers called Tuesday for the country's opposition leaders to face trial and be put to death, a day after clashes between opposition protesters and security forces left one person dead and dozens injured. Tens of thousands of people turned out for the opposition rally Monday in solidarity with Egypt's popular revolt.
"We have sent a strong message to our allies in the region saying, "Let's look at Egypt's example, as opposed to Iran's example," Obama said.
In Egypt, the military, which is now in charge of the country, exercised restraint and did not fire on protesters.
Buoyed by the Egyptian example, protesters also demonstrated in the relatively wealthy country of Bahrain and pressed for the ouster of the ruler in poverty-plagued Yemen.
Obama said there are limits to U.S. influence as people in the Mideast agitate for change.
"These are sovereign countries that have to make their own decisions," he said. "What we can do is lend moral support."
In Iran, in particular, Obama said, "America cannot dictate what happens." But he added that the U.S. hope and expectation "is that we're going to continue to see the people of Iran have the courage to be able to express their yearning for greater freedom and a more representative government."
Demonstrators toppled Mubarak after nearly 30 years in power, handing his authority over to the military on Saturday.
"Obviously there's still a lot of work to be done in Egypt itself," Obama said. But, he added, "What we've seen so far is positive."
He praised the military council that now rules Egypt for reaffirming the 1979 peace treaty with Israel, which has been a lynchpin of regional stability for three decades, and other international treaties. He also praised the leaders for meeting with opposition leaders and working to set the stage for elections and a turnover of the government back to civilian control.
"Egypt's going to require help in building democratic institutions, for strengthening an economy that's taken a hit as a consequence of what happened. But so far at least, we're seeing the right signals coming out of Egypt," Obama said.
Generals from the Armed Forces Supreme Council said Tuesday the military wants to hand power to a government and elected president within six months.
Obama had sharp criticism for Iranian police and government authorities for "gunning down and beating people who were trying to express themselves peacefully."
"An important lesson ... that we can draw from this, is real change in these societies is not going to happen because of terrorism. It's not going to happen because you go around killing innocents. It's going to happen because people come together and apply moral force to a situation. "
"That's what garners international support," Obama said. "That's how you bring about lasting change."
Obama said that the United States in the most part supports the aspirations of pro-democracy activists in protests that are sweeping the Middle East.
"As was true in Egypt, ultimately what happens in each of these countries will be determined by the citizens of those countries. And even as we uphold these universal values, we want to make sure that transitions do not degenerate into chaos and violence."
"That's not only good for us, that's good for those countries," he added.