Iran's president visited a Shiite Muslim shrine early Monday, the final day of an unprecedented visit to Iraq during which he has tried to build ties with a once-hated neighbor while accusing the United States of spreading terrorism.

Mahmoud Ahmadinejad, a Shiite himself, visited the shrine of Imam Mousa al-Kadim around midnight. He traveled in a motorcade under tight security through Baghdad's streets to the shrine in the northern Kazimiyah district.

Ahmadinejad became the first Iranian president to visit Iraq, and his two-day trip illustrated one of the unintended consequences of the 2003 U.S.-led invasion: the replacement of Saddam Hussein, a Sunni who once led an eight-year war against Iran, with Shiite forces closely allied to the cleric-led Islamic republic next door.

Click here for video.

Ahmadinejad said talks Sunday with Iraqi President Jalal Talabani, a Sunni Kurd who told the Iranian leader to call him "Uncle Jalal," were "brotherly." Then Ahmadinejad cut through the U.S.-controlled Green Zone to visit Prime Minister Nouri al-Maliki, a fellow Shiite, at his Cabinet offices.

The sprawling Green Zone contains the core of the U.S. diplomatic mission to Iraq — including a massive new embassy — and is heavily protected against occasional rocket attacks. American officials have accused Iran of backing Shiite extremists behind the rocket attacks.

Ahmadinejad denied the charges during news conferences Sunday.

"Six years ago, there were no terrorists in our region. As soon as the others landed in this country and the region, we witnessed their arrival and presence," he said Sunday night after meeting Abdul-Aziz al-Hakim, the leader of Iraq's largest Shiite political bloc.

Earlier, Ahmadinejad said that "such accusations increase the problems of the Americans in the region. The Iraqi people do not like the Americans."

Ahmadinejad was meeting with Talabani again on Monday, and was scheduled to depart later in the day.

Though both Iraq and Iran have Shiite majorities, they were hostile to each other throughout Saddam's long reign. About 1 million people died in the fighting that ensued after Saddam invaded Iran in 1980.

But when Saddam's Sunni-dominated regime fell to the U.S.-led invasion and Iraq's Shiite majority took power, long-standing ties between the Shiites of both countries flourished.

Ahmadinejad said Sunday he was "very pleased with his visit to an Iraq not ruled by a dictator."

Still, the Iraqis are precariously balanced between U.S. and Iran, with government officials saying in recent weeks that they don't want the country torn apart in a power struggle between the two sides.

The Iranian delegation seemed to enjoy the contrast between Ahmadinejad's visit and trips to Iraq by President Bush.

Ahmadinejad announced the dates of his visit in advance, landed at Baghdad International Airport in daylight and drove through the capital, albeit in a heavily guarded convoy, on a relatively quiet day. Iraqi forces provided security.

In contrast, Bush's visits are typically a surprise and involve trips mostly to U.S. military bases, like his journey to an air base in Anbar province last September.

On Saturday, Bush advised al-Maliki to tell the Iranian leader to "quit sending in sophisticated equipment that's killing our citizens."

Rear Adm. Gregory Smith, a military spokesman, reiterated Sunday that the U.S. hopes the Iranian-Iraqi meetings produce "real and tangible results," which in the American view would include Iran ending its alleged training and funding of extremists.

The tone among Ahmadinejad and his Iraqi hosts, meanwhile, was more than cordial.

"We had very good talks that were friendly and brotherly," Ahmadinejad said after meeting with Talabani, who greeted him with an honor guard and a band that played both countries' national anthems. "We have mutual understandings and views in all fields, and both sides plan to improve relations as much as possible."

After meeting with Ahmadinejad, al-Maliki said the visit was "an expression of the strong desire of enhancing relations and developing mutual interests after the past tension during the dictatorship era."

In contrast to Ahmadinejad's warm welcome from Iraq's government, hundreds of protesters gathered Sunday in Fallujah, the scene of two battles between U.S. troops and Sunni insurgents, and demonstrated for an hour against the Iranian's visit.

"The chieftains of Fallujah condemn the visit of Ahmadinejad to Baghdad," one of their banners read. Another 50 people demonstrated against the visit in northern Kirkuk, and tribal chieftains in the country's Shiite-dominated southern region signed a petition against the visit.

Adnan al-Dulaimi, one of Iraq's most influential Sunni politicians, called for restraint. He said the visit indicated the strong Iranian influence in Iraq but said he hoped it would decrease tension between the two countries.

"We call upon the United States and Iran not to make Iraq a field for their struggle," he said.