In an agreement brokered by the top Shiite Muslim religious figure in Iraq, rebellious cleric Moqtada Sadr agreed Thursday night to withdraw his militia from a contested shrine and other parts of the city of Najaf after three weeks of fighting against U.S. and Iraqi forces, government and religious leaders said. The deal commits the country's interim government to significant concessions.

In exchange for Sadr's compliance, the government pledged to pull U.S. military forces out of Najaf and to allow Sadr, who had been wanted by the former U.S. occupation authority on murder charges, to participate in politics.

"He is as free as any Iraqi citizen to do whatever he would like in Iraq," said Qasim Dawood, a minister of state, after announcing the government's acceptance of the peace plan arranged by Grand Ayatollah Ali Sistani.

The accord was reached on a day when more than 45 people died in a mortar attack and other violence in Najaf and the neighboring town of Kufa, which are about 90 miles south of Baghdad.

Members of Sadr's Mahdi Army, a well-armed militia that numbers in the low thousands, will be allowed to leave Najaf and return to their homes without any sanction, despite having fought against U.S. and Iraqi security forces for three weeks. The agreement allows thousands of Shiite pilgrims to enter the closed-off city in the early hours of Friday morning to visit the shrine of Imam Ali, providing an opportunity for militiamen holed up there to melt into the throng and avoid detection as they depart.

At 6:30 a.m. Friday, authorities in Najaf permitted the pilgrims to enter the city and walk toward the shrine. The crowd, estimated at more than 10,000 people, was searched for weapons by Iraqi police officers at the edge of Najaf's Old City district, where the shrine is located. As they streamed toward the shrine, marchers chanted "God is great" and raised their hands in the air.

Sadr, who has reneged on peace deals in the past, did not issue a statement of acceptance, but senior government officials and a top aide to Sistani expressed optimism that Sadr would comply with the terms of this agreement, which was reached during a meeting between Sistani and Sadr. "Mr. Moqtada Sadr has agreed to the proposals from his eminence, Ayatollah Ali Sistani," said Sistani's top aide, Hamed Khafaf.

At 8 a.m. Friday, a message conveyed from Sadr was broadcast from the shrine's loudspeakers instructing militiamen to depart with the crowd. "Drop your weapons and leave Najaf and Kufa," the announcement said. "You have done a great job."

If Sadr's militiamen leave the shrine -- and the Reuters news agency reported some were turning in their weapons and changing into civilian clothes -- it would end a conflict that has claimed hundreds of lives and roiled Iraq's Shiite majority, who have been concerned that using force to resolve the standoff could damage the gold-domed edifice. "Iraq has achieved a victory today," Dawood said at a Thursday night news conference. "No more fights. Najaf and Kufa will be peaceful cities, free from arms, free from militias."

The U.S. military, which ceased offensive operations on Thursday because of the peace talks, did not withdraw from positions inside Najaf after the deal was announced. Dawood said U.S. forces would be instructed to "draw back" by the interim prime minister, Ayad Allawi, once Sadr's militia departs.

The arrangement was a vivid indication of the enormous clout Sistani wields among Iraq's Shiites. His objections to American plans for Iraq's political transition forced the U.S. occupation authority to make substantial changes on two occasions. But in recent months, some political and religious leaders wondered whether Sistani, a reclusive 73-year-old who believes in the separation of religion and government, was losing followers to Sadr, a mercurial man in his early thirties who lacks Sistani's clerical credentials but plays a more activist form of street politics.

Last week, Sistani's aides demanded that Sadr hand over the keys to the shrine, but Sadr's aides refused, insisting that a transfer had to be done on their terms. The exchange seemed to suggest that Sistani lacked the power to rein in Sadr.

But Thursday's compromise indicated Sistani was still the most influential cleric in Iraq, a man who can force both Sadr and the interim government to yield to his middle-ground approach. When Sistani arrived in the southern port city of Basra on Wednesday after a trip to Britain for treatment of a heart condition, Dawood and another cabinet minister flew to meet him and discuss his peace plan. Shortly after Sistani's police-escorted convoy reached Najaf Thursday afternoon, Sadr came calling.

"Sayyid Ali Sistani has played a very important role in bringing about peace," said Dawood, using the honorific reserved for descendants of the prophet Muhammad.

The deal also revealed the limits of the power of Iraq's interim government. Allawi and other senior officials had sought to avoid any resolution that would allow Sadr's militia to reconstitute itself, favoring the use of force to kill or capture as many militiamen as possible. But because the government could not rely on its security forces alone to deal with the threat, it was forced to seek assistance from the U.S. military. That put the government in an untenable position: If U.S. forces stormed the shrine, Shiites would be outraged, but if they didn't, Sadr's men could drag out the confrontation for weeks.

A senior Iraqi official, speaking on condition of anonymity, acknowledged that Sistani's deal will allow the militiamen to return unchallenged to their homes in Baghdad and other Iraqi cities. "We're going to let most of them get away," the official said.

But the official expressed hope that by ending the standoff and allowing Sadr's supporters to participate in politics, the plan would cause the militia to be weakened and eventually demobilized. "If the shrine is clear, it will help us pursue our main objective of dismantling his militia," the official said.

Other Iraqi officials and Western diplomats in Iraq contend that any deal that allows Sadr and many of his most loyal followers to escape will pose an continuing threat to the interim government. The militia does not have a formal roster of members who can be offered jobs or cash incentives to lay down their weapons. And as long as Sadr, who has been charged with murder in the death of a fellow cleric, remains free to preach and rally his loyalists, he will have the power to reconstitute a militia, the officials and diplomats said.

Under the terms of the agreement, Najaf and Kufa would become "demilitarized zones" that are off-limits to militias and foreign military forces; only Iraqi police and National Guard units would be permitted to patrol the areas. Sistani also demanded that the interim government compensate residents whose homes were destroyed in the fighting.

The senior government official said a date has not been set for the withdrawal of U.S. forces from Najaf. "It is contingent upon Najaf becoming a safe place, free of militants," the official said. "If the standoff is resolved and the militants leave Najaf, then the presence of foreign forces in Najaf will not be necessary."

U.S. commanders in the city said Thursday night that they had not received orders to withdraw.

The peace deal was forged after one of the most violent and chaotic days in the three-week confrontation.

On Thursday morning, before Sistani's return to Najaf, three mortar shells slammed into the grounds of the main mosque in Kufa, killing at least 27 people and wounding 63. The marble courtyard was covered with pools of blood and torn clothing as survivors frantically dragged the wounded to a makeshift first-aid station. Overwhelmed ambulance drivers ferried the wounded to the overflowing local hospital, where relatives wailed next to gurneys carrying bloodied young men.

People at the mosque blamed the U.S. military for the attack, but U.S. military officials denied responsibility. A military spokesman said no operations were being conducted near the shrine.

A short while later, unidentified gunmen fired into a group walking on the main road from Kufa to Najaf. At least 15 people were killed, according to hospital officials.

The shooting caused the marchers to disperse as they sought cover. When a small contingent reassembled, they began shouting: "Where are the religious leaders? Where is the government? They let the Iraqis kill each other."

After Sistani's arrival, there was a shooting in Najaf, as hundreds of his supporters, as well as many Sadr loyalists, tried to converge on the house where Sistani had decamped. Police officials said gunmen in the crowd began firing, prompting the police to return fire. At least 10 people were killed and 38 wounded, hospital officials said.

At Najaf's hospital, an employee told the Reuters news agency: "Go look at the morgue. It's full."

Iraq's Health Ministry put the death toll for the day at 74, with 315 wounded, but that count included militiamen killed in clashes with security forces.

The fighting in Najaf also claimed the life of a U.S. Marine on Thursday, the second to be killed in two days, bringing to 11 the number of American military personnel lost in Najaf since the conflict began on Aug. 5.

Correspondent Karl Vick and special correspondent Naseer Nouri in Najaf contributed to this report.

Grand Ayatollah Ali Sistani, above, met with rebel cleric Moqtada Sadr.Shiites from Baghdad hold up Sistani's picture and chant as they head to Najaf to march to the shrine of Imam Ali.