A car bomb killed 10 people and gunmen executed eight more in Iraq's restive Diyala province on Thursday as the government hosted U.S. Vice President Joe Biden at a ceremony in Baghdad to mark the departure of American troops.

The bomb tore apart a produce market in the mainly Shi'ite town of Khalis, 80 km (50 miles) north of the capital, leaving the street strewn with vegetables, body parts and blood, hours before Biden hailed the end of the war at the ornate al-Faw Palace in the capital.

Because of you and the work (that) those of you in uniform have done, we are able to end this war, Biden told an audience of around 300 U.S. and Iraqi troops.

The remaining 13,000 American troops are due to leave Iraq in the next few weeks, nearly nine years after the invasion that toppled Sunni dictator Saddam Hussein.

Iraqi Prime Minister Nuri al-Maliki, who spoke before Biden at a ceremony in one of the last military bases in U.S. hands, said the withdrawal (of U.S. troops) from all Iraqi lands ... will be considered a historic victory.

Maliki repeatedly referred to the fall of Saddam but did not explicitly thank the United States for ousting him. He thanked President Barack Obama for completing the withdrawal on time and former President George W. Bush for agreeing to it in 2008.

The explosion in Khalis left a three-metre (10-foot) diameter crater and at least ten charred cars and pick-up trucks along with scattered onions, pomegranates and wreckage.

The vehicle, a Kia pick-up, was parked among the other cars, a normal thing in this market, and it was driven by a young man who left it. And just ten minutes later the car blew up, witness Ibrahim al-Temimi said.

The blast killed 10 and wounded 25, said police Major Ali al-Temimi. A physician at the local hospital confirmed the toll.

About 30 km (19 miles) away in the Sunni town of Buhriz, gunmen stormed three houses belonging to former members of the Sahwa militia, Sunni fighters who helped U.S. and Iraqi forces battle al Qaeda.

The attackers lined up family members against a wall and shot them, army Captain Falih Feras said. Eight people were killed and five others wounded.

Blood stained the wall behind the victims of the first and the second families. They were not handcuffed or blindfolded and the killers were facing them at the moment they fired, he said.

Khalis and Buhriz are both located in Diyala province, an al Qaeda stronghold just north and east of Baghdad.

The attacks underscored Iraq's fragile security as the United States leaves a rebuilt Iraqi police force and army to cope with a still-lethal al Qaeda-linked Sunni insurgency and Shi'ite militias supported by neighbouring Iran.

Biden arrived in Baghdad late on Tuesday and in meetings with Maliki and other Iraqi leaders hailed a new phase in relations between the two countries.

In a ceremony at the Umm Qsar naval base in southern Basra province on Thursday, U.S. forces lowered the American flag and handed control of the facility to Iraq.

SECTARIAN WORRIES

While violence has waned since the height of the sectarian bloodbath unleashed by the invasion, militants still kill scores of people every month. October's civilian death toll of 161 was the highest of the year, according to government figures.

U.S. and Iraqi officials have said attacks may rise as U.S. troops withdraw under terms of a 2008 security pact.

Iraqi forces are on high alert for attacks against Shi'ites related to the religious event of Ashura, which commemorates the death of Prophet Mohammad's grandson Hussein at the battle of Kerbala in 680 and defines Shi'ism and its rift with Sunni Islam.

Authorities imposed a curfew on Khalis after the bombing and a police source who asked not to be named said the security operations centre for Diyala province sent a special forces unit to take control of the bombing scene.

The move highlighted mistrust among Iraqi forces. Officials often accuse security leaders of colluding with militants.

The (special) forces dismissed the police of Khalis from the scene because ... this area was supposed to be a secure and well protected area, the source said. And the question is, how

did this car come to be in the market?

(Additional reporting by Alister Bull; Writing by Jim Loney; Editing by Louise Ireland)