At the Central Park Zoo, Marty the zebra is celebrating his tenth birthday, but has grown bored with his daily routine and longs to experience the wild. Marty's best friend is Alex the lion, who enjoys showing off for the public and his celebrity status as "the king of New York". Alex attempts to cheer Marty up, but Marty, still unsatisfied, gets some tips from the zoo's penguins—Skipper, Kowalski, Rico, and Private—who are trying to escape the zoo, and follows them out. Alex, Melman the giraffe, and Gloria the hippopotamus pursue Marty in an attempt to convince him to return. The four, along with the penguins and two chimpanzees named Mason and Phil, find themselves at Grand Central Station, where they are quickly sedated via tranquillizer gun when Alex's attempt to communicate with humans is mistaken for aggression. The zoo, under pressure from anti-captivity activists, is forced to ship the escaped animals by sea to a Kenyan wildlife preserve. During their travels, the penguins escape from their enclosure and take over the ship, intent on taking it to Antarctica. Their antics on the bridge cause the crates containing Alex, Marty, Melman, and Gloria to fall overboard and wash ashore on Madagascar.
The animals are soon able to regroup, initially believing themselves to be at the San Diego Zoo. Upon exploring, however, they come across a pack of lemurs, led by King Julien XIII, and learn their true location. Alex blames Marty for their predicament and attempts to signal for help to get back to civilization. Marty, on the other hand, finds the wild to be exactly what he was looking for, with Gloria and Melman soon joining him in enjoying the island. Alex eventually comes around, but, being separated from the raw steaks he was provided at the zoo, his prey drive begins to show as hunger kicks in. The group is accepted by the lemurs, though King Julien's adviser, Maurice, cautions them about Alex's predatory nature. King Julien ignores Maurice's concerns and persuades the group to help the lemurs fend off the fossa, who hunt the lemurs as prey. While Alex initially scares the fossa away and is worshiped by the lemurs, later, compelled by hunger, he attacks Marty. Realizing that Alex is now a threat, King Julien banishes him to the far side of the island where the fossa live. Seeing what has happened to Alex, and how how difficult it is to survive with so many predators around the island, Marty begins to regret his decision to leave the zoo.
The penguins, having been to Antarctica and found that it "sucks", land the ship at Madagascar. Seeing this as a chance to return Alex to New York, Marty rushes after his friend against the wishes of the others. Marty attempts to convince the now grizzled, starving Alex to return, but Alex refuses out of fear of attacking Marty again. The penguins, Gloria, and Melman go to find Marty, but are trapped by the fossa. At the last minute, Alex overcomes his predatory instincts and scares the fossa away from the lemur territory forever. The lemurs regain their respect for Alex, and the penguins help him satisfy his hunger through sushi. As the lemurs throw a farewell celebration for the foursome, the penguins decide not to break the news that the ship has run out of fuel.