The movie begins by revealing Alex's past. Zuba tries to teach his son Alakay to be a hunter, but the cub is more interested in dancing. When another lion named Makunga challenges Zuba to a fight for the position of alpha lion, Alakay is captured by poachers. Though Zuba tries to rescue his son, Alakay is put into a wooden crate. Zuba is shot in the ear when he tries to get Alakay out of the crate. The crate falls into the ocean where it drifts to New York. There, Alakay is renamed Alex and sent to the Central Park Zoo where he grew up, meeting his new friends Marty, Melman, and Gloria. The events of the first film are then described through news flashes.
In the present time, Alex, Marty, Melman, Gloria, King Julien, Maurice and Skipper and his fellow penguins board a repaired airplane in the hopes of using it to fly back to New York. Mort tries to board the plane but King Julien doesn't want him on board and locks him outside. The plane takes off and starts with a relaxing journey, Alex spots Mort clinging to the wing of the plane and imagines he was a scary creature, but wasn't and waves to him (causing Mort to wave back and fall off the wing into the sea). Alex says that solves spill the resentment of Marty when he the bit and Gloria says woo. Melman is nonconformist. The plane shortly runs out of fuel (or stops burning). Julien and Maurice if eject from the plane. The Penguins decide to land in continental Africa. The animals admit the stuff they kept secret; Alex admits that he broke his iPod (which Marty was furious about) and Melman yells that he loves Gloria, but who hears this but Alex and Marty, and embarrassed, says he likes her as you like the beach or a good book. Luckily for Melman, Gloria was asleep. Eventually, Rico regains control of the plane and lands - but effectively demolishes it in the process.
In Africa, Alex re-encounters Nana and they begin a short fight, but Nana again won the fight, and Alex swipes her handbag and tries to use a cell phone, but fails to get a connection, (despite the fact he was in Africa, and he was distracted by the view of the Watering Hole) the animals are amazed to find more of their kind. Alex is reunited with Zuba and his mom Florrie. Marty quickly fits into a herd of zebra who all look, sound, and talk exactly like him. Melman takes on the position of witch doctor amongst the animals. Gloria, interested in finding a mate, attracts the attention of a smooth-talking, but shallow hippo named Moto Moto.
King Julien resurfaces with Maurice, riding a flamingo and wearing a crown made of flamingo feathers and promptly declares himself king - not that anybody listens much to him.
Meanwhile, the penguins set about repairing the plane with the help of the monkeys. They steal several jeeps from New Yorkers on vacation, leaving the tourists stranded and lost in the jungle. Taking charge of the group is Nana, a tough old woman known for having beaten up Alex before he was first shipped out of New York, reminding them that they are New Yorkers and will always survive.
The next morning Mort washes up on the shore of Africa and sets off to find King Julien, only to be chased by a shark.
Unfortunately, life in Africa is not as wonderful as it first seems. Makunga, still determined to take the position of alpha lion, reminds Zuba that Alex must complete a traditional lion coming-of-age challenge. Alex, thinking that the challenge is a dance contest (it is actually a fight), competes against the strongest lion, "Teetsie" (who was recommended by Makunga) and loses quickly. To avoid being forced to banish Alex for failing, Zuba is forced to abdicate and Makunga immediately takes the position and banishes Alex and his family, much to Zuba's anger at Alex. Meanwhile, Marty begins to feel upset that all of the zebras are exactly the same as him, leaving him with nothing unique.
Melman is happy as a witch doctor, until he learns that he has the same spot that had apparently caused the previous witch doctor to die in what is believed as "Witch Doctor's Disease". He is also upset about Gloria dating Moto Moto, as Melman has secretly loved her for a long time. Believing he is dying, he is confronted by King Julien, who tells him that if he's going to die, he might as well do everything he's dreamed of. Later, Gloria goes on a date with Moto Moto, in a lake, and quickly realizes that he only loves her for her body. Melman rushes up and warns Moto-Moto that he has to treat Gloria well, because he's dating a goddess and she has a beautiful smile. Gloria is surprised, because Moto-Moto is only interested in it being the largest hippo he's ever seen.
The next day, the animals are in a panic when they discover that the watering hole has dried up. Determined to make up for his failure earlier, Alex and Marty decide to risk being shot by hunters and leave the reserve to discover what happened. The pair discover that the stranded New Yorkers, under the instruction of Nana, have dammed the river and built a primitive civilization. Alex is captured and Marty runs for help. Meanwhile, Zuba hears from Makunga what Alex did and goes to rescue him.
Purely to get attention, King Julien suggests that the animals sacrifice one of themselves into the volcano to appease the "water gods" and regain their water supply, and in desperation, the other animals go along with it. Believing that he will die soon, Melman volunteers to be sacrificed. Gloria stops him just in time and Marty arrives to tell them of Alex's fate.
Alex meanwhile manages to rescue both himself and his father by dancing for the New Yorkers, who quickly recognize him from the zoo. Nana however is not won over and attacks. The trio, the penguins, and several chimpanzees use the newly-fixed plane (re-engineered as a military helicopter) to come to the rescue. Then they crash the dam with the plane.
Meanwhile, back at the volcano, King Julien complains to Maurice about the sacrifice plan not working ("The science seemed so solid"). Mort turns up and is overjoyed to see King Julien, although Julien is less than thrilled to see him. The shark that was chasing Mort also arrives, but falls into the volcano, completing the sacrifice (with Maurice saying "I hope the gods like Seafood") and causing King Julien to think that he brought back the water. As the watering hole restores, Makunga angrily reminds Alex and Zuba that all this won't change anything. However, Alex manages to remove Makunga from power by tricking Nana into attacking him and she drags him back to the other marooned tourists. Zuba then makes Alex the alpha lion.
The movie ends as Skipper marries a "bobble-head hula doll from the plane" and the penguins and monkeys leave in the helicopter to go on a honeymoon in Monte Carlo. They leave the Lemurs, Alex, Marty, Melman, and Gloria to happily remain in Africa with Alex's parents for a while.
This sets the stage for Madagascar 3: Europe's Most Wanted.