The closely watched by-election was called after Dr Wan Azizah resigned as the MP on July 31 to enable her husband to run for election.
Anwar held the seat for four terms before he was sacked from his Deputy Prime Minister's post in 1998 under corruption and sodomy charges.
Dr Wan Azizah, who had been known as a reluctant politician, won the seat for a third term in March.
A mammoth crowd comprising PKR leaders and supporters gathered early at the tallying centre at the Tuanku Bainun Teacher Training College in Bukit Mertajam to celebrate Anwar's victory.
Supporters in the crowd let off fireworks as returning officer Roslan Yahaya announced the results about 10pm.
Voter turnout was relatively high, at about 80% of the total of 58,459 registered voters in the constituency.
Barisan was the underdog from the start in the hard-fought by-election that Deputy Prime Minister Najib Tun Razak described as a titanic challenge.
Anwar’s convincing victory was reminiscent of his heyday, when he secured a stunning 23,515-vote majority in the 1995 general election by polling 27,945 votes. In Tuesday's polling, 47,258 voters cast their ballots, or 80.83% of the 58,459 total number of voters.