In Tehran, Putin also held his first face-to-face meeting since the invasion with a NATO leader, Turkey's Tayyip Erdogan, to discuss a deal that would resume Ukraine's Black Sea grain exports as well as the conflict in northern Syria.
Putin's trip, coming just days after US President Joe Biden visited Israel and Saudi Arabia, sends a strong message to the West about Moscow's plans to forge closer strategic ties with Iran, China and India in the face of Western sanctions.
Khamenei called for long-term cooperation between Iran and Russia, telling Putin that the two countries needed to stay vigilant against "Western deception", Iran's state TV reported.
He said Putin had ensured Russia "maintained its independence" from the United States and that countries should start using their own national currencies when trading goods.
"The US dollar should be gradually taken off global trade, and this can be done gradually," Khamenei said during the meeting, in a spartan white room with an Iranian flag and a portrait of late revolutionary leader Ayatollah Khomeini.
Despite the suffering endured by ordinary people in war, Khamenei said Moscow had little alternative in Ukraine. "If you had not taken the initiative, the other side (West) would have caused a war on its own initiative," he told Putin.
Washington said Putin's trip to Tehran showed how isolated Russia had become in the wake of its invasion of Ukraine.
For Iran, also chafing under Western economic sanctions and at loggerheads with the United States over Tehran's nuclear programme and a range of other issues, Putin's visit is timely.
The Islamic Republic's clerical leaders are keen to strengthen strategic relations with Russia against an emerging, US-backed Gulf Arab-Israeli bloc that could shift the Middle East balance of power further away from Iran.
Emboldened by high oil prices since the Ukraine war, Iran is betting that with Russia's support it could pressure Washington to offer concessions for the revival of its 2015 nuclear deal with world powers.
However, Russia's increased tilt towards Beijing in recent months has significantly reduced Iranian crude exports to China - a key source of income for Tehran since then-US President Donald Trump reimposed sanctions in 2018.
In May, Reuters reported that Iran's crude exports to China had fallen sharply as Beijing favoured heavily discounted Russian barrels, leaving almost 40 million barrels of Iranian oil stored on tankers at sea in Asia and seeking buyers.
Ahead of Putin's arrival, the National Iranian Oil Company (NIOC) and Russian gas producer Gazprom signed a memorandum of understanding worth around $40 billion.
Putin, Erdogan and Iranian President Ebrahim Raisi also weighed efforts to reduce violence in Syria, where Turkey has threatened to launch more military operations to extend 30-km (20-mile) deep "safe zones" along the border. Moscow and Tehran oppose any such action by Turkey.
"Maintaining the territorial integrity of Syria is very important, and any military attack in northern Syria will definitely harm Turkey, Syria and the entire region, and benefit terrorists," Khamenei told Erdogan before the three-way meeting.
Russia and Iran are Syrian President Bashar al-Assad's strongest backers, while Turkey supports anti-Assad insurgents.
Any Turkish operation in Syria would attack the Kurdish YPG militia, a key part of the US-backed Syrian Democratic Forces (SDF) that controls large parts of north Syria and is regarded by Washington as an important ally against Islamic State militants.
"It is impossible to expect Turkey to remain inactive, unresponsive, as this terrorist organisation continues its separatist agenda," Erdogan said, referring to the YPG.
"I hear from you, my dear friends, that you understand Turkey's legitimate security concerns. I am thankful for this, but words alone are not a cure for wounds."
Speaking at the end of the talks, Putin said the three presidents agreed to continue consultation on Syria and were committed to maintain efforts to "normalise" the situation there after a decade of conflict.
Putin, who turns 70 this year, has made few foreign trips in recent years due to the COVID-19 pandemic and then the Ukraine crisis. His last trip beyond the former Soviet Union was to China in February.
Russia, Ukraine, Turkey and the United Nations are expected to sign a deal later this week aimed at resuming the shipping of grain from Ukraine across the Black Sea.
"With your mediation we have moved forward," Putin told Erdogan after their bilateral meeting. "Not all the issues have been resolved yet, but the fact that there is movement is already good."