By Drazen Jorgic and Humphrey Malalo
NAIROBI (Reuters) - Defeated presidential contender Raila Odinga challenged his election loss in court on Saturday, alleging widespread ballot rigging in a fresh test of Kenyan democracy five years after a disputed vote triggered deadly tribal violence.
Shortly before the petition was filed, police outside the Supreme Court fired teargas to disperse a rally of around 100 Odinga backers. They were urged by the outgoing prime minister to stay calm and trust in the law to resolve his complaint.
Odinga's complaint threatens to prolong the period of uncertainty shadowing east Africa's largest economy.
Analysts say a swift, transparent resolution of the row will be critical to restoring Kenya's reputation as a stable democracy. Big Western donors worry about a nation seen as a vital ally in a regional struggle against militant Islam.
Odinga, head of the CORD coalition, refuses to accept the slim first-round election win by Uhuru Kenyatta, who has been indicted by the International Criminal Court over the explosion of bloodshed in 2007 that left more than 1,200 people dead.
The March 4 election and aftermath was largely peaceful by contrast. Kenyatta declared the vote "free and fair" in his victory acceptance speech, though he added that the electoral process could be made more refined and efficient in the future.
Kenya's electoral commission (IEBC) had promised a smooth election but the collapse of an expensive new electronic voting system led to a five-day wait for the winner to be announced.
Odinga's petition alleges widespread rigging and accuses the IEBC of inflating voter registration numbers and going ahead with the election aware that its systems were going to fail.
"These failures dwarf anything Kenyans have ever witnessed in any previous election," Odinga told reporters on the doorstep of his office in the centre of the capital Nairobi.
"Every mechanism and every instrument the IEBC deployed failed miserably. Its failure and collapse, on a catastrophic scale on the polling day, so fundamentally changed the system of polling and the number of votes cast."
Kenyatta comfortably beat Odinga in terms of votes won, 50.07 percent versus 43.28 percent, but only narrowly avoided a run-off after winning just 8,100 votes more than the 50 percent needed to be declared the winner outright.
That slim margin has given Odinga allies confidence that they can force a run-off through the courts, though the petition calls for the whole process to be declared null and void.
Kenyatta said his Jubilee coalition would respect the rule of law and the outcome of the petition filed by CORD. "If the decision is not in our favour, then we are ready to face the electorate again," Kenyatta said.
By the time the petition was filed in the early afternoon, hundreds of Odinga supporters gathered outside the court, many wearing T-shirts with slogans such as "democracy on trial".
"I am not happy with the election results, since my rights have been stolen. President-elect Uhuru Kenyatta is not my choice," Florence Bolo, an Odinga supporter, said outside the Supreme Court. Others shouted: "Uhuru must go!"
"We Kenyans want justice and I am crying for the Supreme Court to look at this case as critical and come out with a fair judgement," said Hamsa Omondi, a 21-year-old street vendor.
Traffic was moving freely through Nairobi and there were no signs of further unrest in the capital.
Many had feared a repeat of the 2007 violence after this month's vote, and were relieved when that did not transpire.
Kenya's stock market, which surged 7 percent in the two days after the peaceful conclusion of the March 4 vote, then suffered three consecutive days of falls. Traders said the Supreme Court petition had unnerved foreign investors.
The IEBC has consistently described the vote as credible despite a series of technological glitches on voting day and during the tallying of ballots. The IEBC spokeswoman could not be reached for comment.
International monitors, commenting shortly after counting began, said the election was broadly credible up to that point. But the count went for five days, and monitors did not follow the entire count process, diplomats say.
TEST FOR REFORMED COURTS
Odinga's petition names four respondents - the IEBC, its chairman Issack Hassan, Kenyatta and his running mate William Ruto, who is also under ICC indictment for crimes against humanity over the 2007 bloodshed. Both deny the charges.
Odinga's attempts to nullify Kenyatta's victory will be the first significant test for Kenya's new Supreme Court, established under a constitution adopted in a 2010 referendum.
Chief Justice Willy Mutunga, appointed in 2011 to reform a legal system accused of serving the interests of the elite, will be under pressure to deliver a transparent verdict in a country where political tensions are more tribal than ideological.
Mutunga received death threats in the run-up to the vote but promised the judiciary would act without "favour, prejudice or bias" when handling election complaints. He invited the media to cover the proceedings live from the court.
Odinga's decision to contest the election outcome in the courts was a major departure from his response to the 2007 election outcome which he also described as rigged.
Odinga at the time summoned supporters out into the streets for peaceful protests, as he did not trust the judiciary to be fair. But violence quickly erupted between tribes backing competing leadership candidates and spread across Kenya.
(Writing by Drazen Jorgic; Editing by Mark Heinrich)