"Iraq is facing an existential threat but it can be overcome by the formation of a thoroughly inclusive government," he said at a joint press conference with Prime Minister Nuri al-Maliki.
"It must be a government in which all Iraqis feel represented," said Ban, who has been touring the region mainly in a bid to quell the bloodshed in Gaza and whose Baghdad stop was not initially scheduled.
His visit came hours after a breakthrough in what has been a protracted and acrimonious process to renew Iraq's leadership.
Iraq's Kurds traditionally get the post of president and earlier today agreed on a common candidate, a move which could pave the way for a deal on the much more powerful position of prime minister, which goes to a Shiite Arab.