"Since the two consolidated petitions have failed, Umaru Yar Adua and Goodluck Jonathan [the vice-president] remain validly elected," James Ogebe, the presiding judge, ruled.
The two challengers plan to appeal to the Supreme Court, which could take several more months to deliver a final ruling.
Abubakar, a former vice-president, said he was disappointed.
"My reaction - and that of most Nigerians - is that of disappointment," he told Al Jazeera.
"Our case is very strong," he said. "The court admitted ... the laws of the country were breeched but they said they were not breeched enough to annul the elections."
Reacting to the ruling, Buhari told reporters outside the court: "For us, we have come to the semi-final. We are proceeding to the Supreme Court."
Buhari, who governed Nigeria for 18 months from 1983 to 1985, contested the 2007 presidential election for the All Nigeria People's party.
But he has pursued his petition to have Yar Adua's election overturned without the party's participation.
He also lost the 2003 presidential vote and he pursued his appeal against that ruling until the bitter end.
Yar Adua's disputed victory and doubts over whether he would finish his four-year term have delayed policy-making and investment decisions.
But Tuesday's ruling has been welcomed by many analysts.
Razia Khan, regional head of research for Africa at Standard Chartered Bank in London, said: "From a political stability perspective, this is likely to be positive.
"We expect the decision to be positive for the performance of Nigerian markets."
Analysts said Yar Adua would now be under pressure to deliver on key policies.
Graham Stock, Africa strategist at JP Morgan in London, said: "The key is whether the greater political certainty translates into more effective government. There is not a lot the government can point to after nine months in power."
Nigeria's 2007 elections were marred by fraud countrywide, local and international observers have said the polls were "not credible" as a result.