Riyadh cut ties with Tehran after Iranian protesters attacked Saudi diplomatic missions in the Islamic republic in 2016 following the Saudi execution of revered Shia cleric Nimr al-Nimr.
They said in a joint statement that they had agreed to restore ties and to reopen diplomatic missions within two months after five days of unannounced talks in China.
"A new page has been opened in diplomatic relations between the two countries," the Iraqi foreign ministry said.
The agreement between Iraq's neighbours would provide "impetus for the cooperation of countries in the region", it said in a brief statement.
Iraq had hosted several rounds of reconciliation talks between Iran and Saudi Arabia since April 2021.
Those encounters were held at a relatively low level, involving security and intelligence officials.
Shia-majority Iran and mainly Sunni Saudi Arabia support rival sides in multiple conflicts across the Middle East, including in Yemen where the Houthi rebels are backed by Tehran, and Riyadh leads a military coalition supporting the government.