Malaysia's ruling coalition coasted to victory in two by-elections, defying a political movement led by former premier Mahathir Mohamad.