Malaysia’s defeated leader Najib Razak said on Thursday he accepted the will of the people after his long-ruling coalition suffered a shock election loss to 92-year-old former strongman Mahathir Mohamad.

"I accept the verdict of the people," the leader, who looked shattered after his coalition's defeat, said. But he added that because no single party got a majority in parliament, it was up to the king to decide who will become prime minister.

Mr Mahathir's opposition won 121 seats, if a small ally from Sabah state is included. There are 222 seats in parliament and a majority is needed to form a government.

The veteran ex-leader stormed to a shock victory at the hard-fought election early on Thursday, beating Mr Najib's Barisan Nasional (BN) coalition that has been in charge for over six decades.

The thumping victory capped a dramatic political comeback for Mr Mahathir, who previously ruled the country with an iron fist for 22 years and came out of retirement to take on Mr Razak after the leader became embroiled in a massive corruption scandal.

When he takes power, Mr Mahathir will be the oldest prime minister in the world.

Despite the shock result there were no reports of trouble on the streets, where pockets of celebration erupted overnight.

A flag-waving crowd of supporters gathered on a field outside the headquarters of Mr Mahathir's party near Kuala Lumpur.

Suva Selvan, a 48-year-old doctor, said he felt the country had just won its independence.

"I feel that with this change we probably can see something better in the future … our hope for the future is a better government, fair, free and united," he said.

Defeat could just be the beginning of Mr Najib's troubles. Mr Mahathir has vowed to bring him to justice over allegations that billions of dollars were looted from sovereign wealth fund 1MDB, which the scandal-hit leader set up and oversaw.

But at a press conference after his win, Mr Mahathir vowed: "We are not seeking revenge. We want to restore the rule of law."

He said he would be sworn in Thursday.

Mr Mahathir's return to the political front lines saw him throw in his lot with an opposition alliance filled with parties that he crushed while in power, and which includes jailed opposition icon Anwar Ibrahim — his former nemesis.

Mr Anwar is due to be released from prison in June. Mahathir has vowed to get him a royal pardon, and later hand over the premiership to him.

As well as seizing control of the national government, several state legislatures across the country fell into opposition hands for the first time, including the highly symbolic bastion of Johor, the birthplace of Mr Najib's party that was the linchpin of the ruling coalition.