Mahathir won the election after his coalition Pakatan Harapan secured 113 seats out of the 222 seats at the parliament's lower house, unseating the Barisna Nasional coalition, which has run the country for more than 60 years and of which Mahathir used to serve as the chairman.
TV live-broadcast showed that Mahathir, wearing traditional malay attire, pledged to serve the country and the people well.
He was accompanied by his wife Siti Hasmah Mohamad Ali and other leaders in the opposition alliance, including Wan Azizah Wan Ismail, whom Mahathir pledged to tap as the deputy prime minister.
The ceremony, held at the national palace, was witnessed by Malaysia's head of state Sultan Muhammad V.
A statement released by the royal palace ahead of the ceremony said the king Sultan Muhammad V invited Mahathir to form the next federal government.
The royal palace also refuted allegations that the king delayed the appointment of Mahathir as the new prime minister, saying that he strongly supports and respects the democratic process.
The appointment of Mahathir as prime minister added more optimistic sentiments and jubilance to the country, with many Pakatan Harapan supporters taking to the street, holding flags and honking horns.
In his first press conference as the new prime minister, Mahathir said that even he himself did not anticipate the surprising election results. He thanked the Malaysian people for their support.
Mahathir listed his 22 years of experience being the former prime minister as his qualification to run the country again, saying he aimed to set up a good government, which is based on the constitution and the law.
As for the next move, he said he would consult with leaders of other component parties in the alliance on Friday, to divide the number of ministers among themselves to set up a new cabinet as soon as possible.
Mahathir reiterated his pledge to seek a royal pardon for Anwar Ibrahim and hand over the premiership within two years. Anwar is the former deputy prime minister of Mahathir who is in prison for sodomy charges. He is scheduled to be released in June.
Expectations are high for Mahathir to transform the Malaysian government from the race-dominated agenda to more constructive policies. During his last premiership, Mahathir introduced the "Vision 2020" blueprint for the nation to become a self-sufficient industrialized nation by 2020.
On his way to the royal palace on Thursday afternoon, Mahathir sat in a Malaysian-made sedan called Proton Perdana with car plate number "2020", a sign that he is still sticking to his plan.