The victories allow Prime Minister Najib, under pressure to resign over a graft scandal at state fund 1Malaysia Development Berhad (1MDB), to further tighten his grip over the country and within the ruling Barisan Nasional (BN) pact.
Najib's party won the Sungai Besar and Kuala Kangsar parliamentary seats on Saturday with increased margins compared with those in 2013 general elections, the election commission said. Both areas are mostly made up of farming and fishing communities along peninsula Malaysia's west coast.
"Tun Mahathir Mohamad turned the elections into a referendum on my leadership," Najib said in a statement.
"They (the people) rejected Tun Mahathir's lies, they rejected his unworkable coalition of former enemies, and they rejected the incoherent opposition - partly because of their alignment with Tun Mahathir," he said.
While the outcome of the by-elections would not tilt the balance of power in parliament, observers noted that it may offer clues as to whether Mahathir's influence is waning.
Najib's alliance secured a landslide win in the Borneo state of Sarawak last month, and political experts feel he may call snap polls to consolidate his strong position.
James Chin, director at the University of Tasmania's Asia Institute, told Reuters there was a "high probability that there will be a snap polls if BN wins by big majorities."
The next general election is scheduled for 2018. The coalition lost its two-third majority in the 2008 polls, and Najib lost the popular vote in 2013 despite BN retaining power.
Najib's critics have demanded his resignation after reports claiming billions of dollars had been misappropriated through his pet project 1MDB, and that up to $1 billion was deposited into his personal bank account. Najib has denied the reports.
Mahathir, who led the country for 22 years until retiring in 2003, quit the United Malay National Organisation (UMNO) party earlier this year in disgust over Najib's grip on the party despite the allegations surrounding 1MDB, which is now the subject of international probes in at least six countries.