Voter turnout was 82 per cent, among the highest in Malaysian by-election history.
Anwar, a former deputy prime minister, was arrested for leading a protest in 1998 and jailed for six years on what he called trumped-up charges that included sodomy.
Banned from standing for office until April 2008 due to his criminal record, he has now been seeking to re-launch his political career.
After Saturday's announcement, Anwar said: "I'm sad for Malaysia. I have been talking about the fraudulent process all the while but I didn't think it was going to be so bad ... the intimidation, the blatant bribery."
Najib Razak, the deputy prime minister, said it was a significant victory because Anwar "resorted to aggressive campaigning".
He said: "They threw all sorts of allegations at Barisan Nasional leaders."
Asked if the victory would pave the way for early elections, he said: "No, but it is a good precursor to general elections, though the timing depends on a lot of other factors."
There had been reports of sporadic clashes between rival factions in the run-up to the polls, the toughest electoral test for Abdullah since he took office in late 2003.
But while not running in the election, he has been campaigning in a high-profile race against Barisan heavyweights in what is seen as a proxy national battle.
Outside the town hall, opposition supporters chanted "Reformasi", a war cry of Anwar's reform movement of the late 1990s.
During the poll, his supporters stopped two buses they claimed carried "phantom" voters, or people posing as voters who are dead but whose names still appear on electoral lists.
The by-election had been a test of the political currents that appear to be sweeping Abdullah towards an early election.
A general election is not due until 2009, but Abdullah is expected to hold an election before Anwar's political ban is lifted and he can stand for parliament.