A crazed man threatened to detonate a bomb aboard a Malaysia Airlines plane, and tried to enter the cockpit — but was thwarted by a group of “heroic” passengers, officials said.

Manodh Marks, a 25-year-old Sri Lankan, had been released from a Melbourne psychiatric hospital Wednesday before buying a ticket on the late-night flight to Kuala Lumpur, Malaysia, Victorian Police Chief Commissioner Graham Ashton said.

Flight 128 was about 10 minutes into a trip from Melbourne when Marks walked from his economy seat to the cockpit door holding an electronic device and threatened to blow up the jetliner, sparking panic among the passengers.

One passenger said the lunatic was carrying a “very unusual object,” which police later said was not explosive.

“He ran down the back aisle and three great Aussie heroes wrestled him to the ground and totally immobilized him,” Andrew Leoncelli, a former Australian rules football player, told the BBC.

Police said Marks was carrying a Bluetooth speaker or something similar.

Leoncelli described it as a portable music player.

“I’ve never seen anything like it. It was the size of a watermelon, it was huge, it was black,” he told radio station 3AW. “It had two sort of like antennae stuff coming off it, but it also looked like it had an iPhone jack in it, so it could have been just like a beatbox thing.

“He was saying, ‘I’m going to the blow the f–ing plane up! I’m going to blow the plane up!’” Leoncelli said. “He was agitated, is the best description — 100 percent, he was agitated.”

Police Superintendent Tony Langdon credited flight crew with also playing a part in tackling the man.

“We believe that the actions of the passengers and crew were quite heroic,” Langdon said.

The Airbus A330-300 carrying 337 passengers returned to Melbourne Airport about 30 minutes after takeoff.

Passengers were kept on board for 90 minutes after landing and the plane was searched for potential bombs at a remote part of the airport, Ashton said.

Police determined Marks — who is studying to be a chef in Australia on a student visa — had no “terrorist” links or associates, Ashton said.

Marks was charged with attempting to enter the cockpit and threatening to detonate an explosive device contrary to the Crimes (Aviation) Act, The Australian reported.

He chose not to appear in Melbourne Magistrates court, where he was charged with the federal offenses that carry a 10-year prison term, the news outlet reported Thursday.

He asked for medical attention and also requested special treatment while in custody, his lawyer said.

“He has concerns for his safety in custody,” his lawyer told Magistrate Susie Cameron.

“He needs to see a mental-health nurse urgently,” she responded.

Malaysia’s state-owned airline has had two recent high-profile disasters.

Malaysia Airlines Flight MH17 was shot down over Ukraine in 2014 with the loss of all 283 passengers and 15 crew. And Flight MH370 with 238 people aboard disappeared four months earlier. It is believed to have crashed in the southern Indian Ocean but has not been found.

With Post wires