“It was a remarkable result after Ferrari's struggles at the start of the season - and it was matched by Perez's performance”
Perez caught him in the closing stages but ran wide and settled for second.
McLaren's Lewis Hamilton took third ahead of Red Bull's Mark Webber and Lotus driver Kimi Raikkonen.
Alonso built a seven-second lead after the race resumed following a stoppage after just six laps caused by a torrential downpour.
But, as the track dried, Perez closed the Spaniard's lead to virtually nothing.
Alonso gained some respite when he switched to dry-weather slick tyres a lap before Perez, which took his lead back up to seven seconds.
After fitting the 'hard' tyres while Alonso chose the 'medium', Perez closed the gap to nothing in 10 laps and was on Alonso's tail at the start of lap 50, with seven to go.
But no sooner had Perez caught the Ferrari than he made a mistake.
He got on to the kerb at Turn 14 and ran wide, and suddenly Alonso's lead was back up to five seconds.
Although Perez closed in again, Alonso now had enough of a cushion to take a 28th career victory - behind only Michael Schumacher, Alain Prost, Ayrton Senna and Nigel Mansell in the all-time list.
Alonso, who is now leading the world championship by five points from Hamilton, said the victory was "a big surprise".
"We were not competitive in Australia or here," he added. "Our goal was to score as many points as possible and we did the job.
Fernando Alonso delights in winning 'difficult' Malaysian GP
"It is an unbelievable result, a great job from the team. We maximised the potential in our hands, keeping calm in some strange conditions at the beginning with inters and the switching to dry tyres.
"The team deserve this win. It's a tough time for us at the moment, but this is a Sunday we will remember."
Perez said he felt he had missed an opportunity to win.
"I was catching Fernando towards end of the race," he said.
"I knew I had to get him soon because in all the high-speed corners I was losing my front tyres running behind him.
Malaysian GP facts
Nine different constructors finished in the points
Ave wind speed:
2.7 metres per second
Kimi Raikkonen (lap 53): 1min 40.722 secs
Nico Rosberg 295kph (183mph)
: Fernando Alonso 138kph (86mph)
: Daniel Ricciardo 275kph (171mph)
Jean-Eric Vergne 299kph (186mph)
"Then I ran wide in the quick left-hander. I touched the kerb and I went onto the dirty side of the track. It was completely wet and I lost the win."
It was a remarkable result considering Ferrari's struggles with their car - and it was matched by Perez's performance.
The race started on a damp track after a shower of rain, with most drivers on the 'intermediate' tyres with relatively light treads.
But heavy rain started on lap two, and Perez immediately dived into the pits to fit full wet tyres.
It worked to the Mexican's favour. As the leading drivers pitted for wet tyres on laps four and five, he moved up the field and was lying third behind the two McLarens of Hamilton and Button when the race was stopped on lap six - when Alonso was fifth behind Webber.
But the decisive part of the race came after the re-start, as Alonso emerged in second place after all the drivers had stopped to fit intermediate tyres and immediately passed Perez for the lead on lap 16.
He pulled away in relentless fashion, the fastest car on the track for several laps, building what turned out to be a decisive lead of 7.7 seconds on lap 30, before Perez began to come back at him.
Alonso's experience told in the end, but both men celebrated one of the most remarkable and unexpected results in recent F1 history.
Third-placed Lewis Hamilton said: "I think we can be satisfied. First of all congratulations to Fernando and Sergio, they both drove fantastically well and it was very difficult to catch them.
"I can't really complain, I was on the podium for the second race in a row."
This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.