Maliki said through the landmark visit the Iranian leader expressed "a strong desire to boost the relations and push forward common interests."
The two neighboring countries fought an eight-year bloody war in the 1980s, which cost about one million lives. After the fall of the Sunni regime of Saddam Hussein toppled by the U.S.-led invasion in 2003, the two Shiite governments have been moving closer.
Many leading Iraqi Shiite politicians sought refuge and support from Iran during the rule of Saddam, including Maliki.
Earlier in the day, Ahmadinejad told reporters that the two sides signed seven agreements, covering areas like trade, customs and transport.
"This was a great step toward better relationship between the two countries," said Ahmadinejad, the first Iranian president to visit Iraq since Iran's 1979 Islamic Revolution.
Analysts said by helping the Iraqi Shiites consolidate control, it would be easier for Tehran to keep its influence here.
The U.S. is concerned that Iran's greater role in Iraq would endanger its say here. It also accuses Iran of arming, training and financing Iraqi Shiite militia to attack U.S. troops, which Iran denies.