The movie starts in the past, in an African reserve, when Alex the Lion was just a little cub, being tutored in the art of being a lion, by his doting father, Zuba, who is also the Alpha Lion. Alex is full of life and more interested in playing the fool, by dancing around and doing the "happy face/angry face" gag with his paw. Zuba shows him his paw, which has an Africa-shaped birthmark on it, which Alex also has on his paw. Just then, Makunga (Alec Baldwin), a scheming lion who wants to be Alpha Lion, challenges Zuba. Leaving Alex alone for a while, Zuba kicks the pride out of Makunga. As the fight is going on, Alex notices a piece of rope moving on the ground and follows it. He goes off the reserve and is captured by poachers, who dump him in a crate and put him in the bed of their truck. Zuba notices that his son is missing. He sees the truck moving along the fence of the reserve and hears his son, crying out for help. He runs and jumps on the truck. He manages to free the crate of its binds, when one of the poachers shoots him, causing him to fall off the truck. He doesn't notice that Alex's crate has fallen out of the truck-bed and into a nearby river. Despite the painful hole in his ear, he mourns the loss of his son.
Meanwhile, in the box, Alex sits timidly. Finally, he reaches New York City and is fished out and put in the New York City Zoo. When he sees all the people looking at him, he starts to dance around, much to the audience's pleasure. As he vamps for the people, behind him, a young Marty the Zebra tells a young Gloria (the Hippo) and Melman (the Giraffe) that Alex is a show-off. Years later, Alex is still a huge hit with the zoo spectators. Marty (Chris Rock) tells Gloria (Jada Pinkett Smith) and Melman (David Schwimmer) that he still thinks he's a show-off. Marty then expresses his desire to go out in the wild...which, in his mind, means Connecticut.
Then, we see a TV news report on the four animals' NY escapade (from the previous movie) and their encounter with the fearsome Nana (Elisa Gabrielli). After they were recaptured, animal rights activists demanded that they be sent back to the wild. During their voyage, their ship went missing, much to the sadness of New Yorkers.
Next, in the island of Madagascar, we see that the four animals are now preparing to go back home, thanks to the airplane that the penguins supposedly fixed. As they climb up to the huge platform that the lemurs built, they groove to the tune of "I Like to Move It". They exchange warm goodbyes to the lemurs, at least most of them. King Julien (Sacha Baron Cohen), dressed as a lady in a cake, and Maurice (Cedric the Entertainer) are coming along. When the creepy little Mort tries to come along, Julien turns security loose on him and herds everyone into the plane. The two monkeys, Mason and Phil, are also there. With the lemurs' help, the plane is catapulted into the air.
As they fly through a storm cloud, Alex is scared by what appears to be a gremlin. However, when the sun comes out, he sees it's only Mort. When Mort waves to Alex, he loses hold of the wing and is blown off, leaving Alex to think it was his imagination. Inside the plane, the animals talk about Madagascar. Gloria mentions that, when they reach NY, she might get into the breeding program, which makes Melman a bit jealous. She shrugs it off and puts on a mask to take a nap. In the front of the cabin, Julien and Maurice sit in "first-class", eating shrimp.
As Skipper talks to a hula dancer-shaped bobblehead doll, the plane runs out of fuel. Although Skipper initially solves the problem by smashing the warning light with the manual, the engines give out, and the plane falls out of the sky. As Marty, Alex and Melman scream in terror, King Julien treats it like a roller coaster ride. Their hatch falls open, and he and Maurice are sucked out, with a parachute deploying, causing Julien to yell out, "I can fly!" As Marty and Alex say their goodbyes, Alex confesses that he broke Marty's iPod back at the zoo. Marty's brief anger, however, is interrupted when Melman blurts out that he loves Gloria. Gloria is still asleep, and he has to backtrack on it to his friends.
Right before they crash, Skipper orders Rico to pull up (he had been diving the plane just for fun), and they skim the ground. The landing gear deploy, but break off because the plane is moving too fast. The plane skids through a forest, the wings stripping off, leaving just the airframe. When it flies off a cliff, the penguins deploy parachutes, leaving the plane's carcass to float to the ground, where the oxygen masks finally deploy. Finally, Gloria wakes up, and finds Melman hyperventilating in one of the masks.
When the dust settles, they look around, wondering where they are. Skipper and the other penguins celebrate their landing, being able to "live with" two unaccounted for (Julien and Maurice). Skipper tells the animals that they will need 6-9 months, with the monkeys' help, to rebuild the plane. Alex and Skipper initially quarrel over the repairs (with Private pulling a switchblade knife at one point), but Alex eventually gives up and leads his friends away to contemplate their fate.
Initially believed to be in the middle of nowhere, they are proved somewhat wrong when a jeep approaches, filled with tourists who click pictures, then quickly drives off. Alex runs after the jeep, trying to get help, but is attacked by Nana (who's on the jeep). After a brief, comical fight with her, he ends up on the losing side. The jeep drives off, but Alex got her handbag. He takes her cell phone and is about to call someone, when they look down the cliff they're on and see several animals - hippos, lions, zebras and giraffes, among others. Although Melman initially thinks again that they're at the San Diego Zoo, they quickly realize they're in Africa. (Though it's a good thing they realize they're "home"...the cell phone has no service.)
They go to the watering hole, where the animals are assembled. Alex tries to make contact with the natives, but quickly finds out they can understand him just fine. They ask how the four of them got into the reserve. At that moment, the Alpha Lion, Zuba, and his mate (Sherri Shepherd) approach. Zuba doesn't recognize Alex, though Alex feels he's seen Zuba before. Suddenly, Zuba's mate recognizes Alex and looks at his paw, with the Africa-shaped birthmark on it. Zuba and Alex's Mom are overjoyed at the return of their son, as is Alex at being reunited with his parents. All the animals around rejoice the return of Alex, the "King of New York". And, surprisingly, so does Makunga. He is going to use Alex against Zuba, to become Alpha Lion. Julien and Maurice show up and partake in the celebrations, thinking it's for them.
Marty meets the other zebras (who all sound exactly like him). They welcome him into their fold. Gloria meets the other hippos and becomes the object of attraction for Moto Moto (Will.i.Am), the hippo "hunk". Melman meets the giraffes and asks about medications. They say that now that their witch doctor, Joe, is dead, if any giraffe gets sick, they go into a hole and wait for death. Horrified, Melman volunteers to become the giraffe's witch doctor.
Makunga speaks to Zuba about banishing Alex as he has not performed the rite of passage that all young lion cubs should perform. Alex, thinking it to be a breeze, accepts. Meanwhile, Marty finds out that his "water-spit" trick is no longer exclusive to him, as all the other zebras learn it immediately. He is surprised to see that all zebras are exactly like him. Melman, after having helped out a young giraffe with a broken leg, is informed that the brown spot on his neck is a symptom of "witch doctor's disease" and he has 48 hours to live.
Meanwhile, the penguins hijack safari jeeps to use to fix the plane, rendering many of the tourists (including Nana) stranded in the African reserve. The monkeys bring a whole bunch of monkeys as extra help for fixing the plane. Nana (having just gotten her beloved handbag back) leads the stranded tourists, who are all from New York, and tells them that, as New Yorkers, they can survive anything.
Alex prepares for the rite of passage, thinking it to be a dance-off. As he prepares, Makunga comes along and talks him into choosing Teetsi, who Alex thinks will be his dance partner. When the time comes for the rite of passage, Alex does indeed choose Teetsi, who turns out to be something like the Incredible Lion Hulk. Alex still thinks it's a dance-off and dances around, to the confusion of his father, until Teetsi levels Alex with a punch. Now that Alex has been defeated, Makunga says that Alex should be banished. Unable to do it himself, Zuba tosses away the Alpha Lion scepter and abdicates. Makunga obligingly decides to become Alpha Lion. Alex is made to wear a ridiculous fruit hat, as his punishment.
Zuba berates his son for shaming him. Alex walks off in a huff. In the wreckage of the plane, Alex, Marty and Melman sit, moping. Alex is sad about being banished, Melman is distressed that he has only 2 days to live and Marty is upset he's exactly like all the other zebras. Gloria comes along happily and gloats about the great time she's having. When she mentions Moto Moto and his interest in her, Melman gets jealous and they quarrel and stalk off in separate directions. Alex and "Marty" are left behind, until the real Marty comes along, and is offended that Alex could not tell the two zebras apart. Accusing Alex of only thinking of himself, he walks away.
As Melman sits inside a giraffe death hole, Julien and Maurice come to him. Julien advises Melman to tell Gloria how he feels. Gloria is at the hippo's watering hole, being charmed by Moto Moto. She asks why he likes her. Besides her plumpness, he can't answer anything else. Melman comes over and sees them together. He tells Moto Moto to take care of Gloria, and expresses his love for her in a way far more profound than the male hippo is capable of, before leaving, having shocked Gloria with his feelings.
Inexplicably, the watering hole dries up, leaving only a tiny amount. Makunga comes along and suggests that the animals fight each other for the remaining water. Alex shows up and says that he will go and look for the reason for the water stoppage, which is off the reserve. If any animal goes off the reserve, it is fair game for hunters. Makunga, hearing the dissent among the other animals, says that he will confer with Zuba. Before leaving the reserve, Alex goes to the zebra herd and looks for Marty. Unable to find him, he shouts out that he's sorry. However, he recognizes Marty (the only one not facing him, revealing the scar left from his bite mark in the last movie) and asks him to accept his apology. Having made up, Marty accompanies Alex into the woods outside the reserve. Meanwhile, Makunga tells Zuba and Mom that Alex has gone out of the reserve. Zuba runs off immediately in search of his son, while Makunga laughs evilly.
At the watering hole, Moto Moto tries to dig for water, but only finds diamonds and gold. Julien and Maurice show up and declare that an animal sacrifice to the volcano will please the water gods. They need a volunteer. Melman, thinking that he has less than a day to live, volunteers. Cheering, the animals lift him up and take him to the volcano. Gloria protests, trying to talk Melman out of it, but he has made up his mind.
Alex and Marty go through the eerie woods and come up to the source of the water blockage - a dam erected by the New York tourists, led by Nana. They are suddenly attacked by spear-wielding tourists. Alex is caught in a rope trap. He tells Marty to go get help. Marty runs off.
At the volcano, Melman stands shakily at the edge of the precipice, overlooking the red-hot magma. Suddenly, the old witch doctor, Joe, shows up. He says that all giraffes have brown spots, so there's no such thing as "witch doctor's disease". Gloria runs up to Melman and tells him not to jump. However, as she runs up to the precipice, she stumbles and cracks it. Melman starts running towards her, past the crumbling precipice. She grabs him by his horns. They profess their love for each other. Just then, Marty runs in and tells them that Alex is in trouble and they need the plane.
At the crash site, the monkeys are on strike, Mason and Phil are negotiating with Skipper, and demanding maternity leave (even though the monkeys are all males). Gloria, Marty and Melman run up and tell them to get the plane ready immediately, but Skipper can't until he "breaks the union". However, Phil comes up with "incriminating" photos of Skipper and his bobblehead doll. Skipper grants their maternity leave, and the monkeys get right to work.
Alex is taken to the tourists' colony, where they plan to roast him for dinner. Just then, Zuba shows up and frees Alex. Seeing that the tourists are New Yorkers, Alex starts doing his famous dance. The tourists recognize Alex, who they thought was dead. At first bewildered, Zuba also joins in the fun. However, Nana still wants to eat them and aims the shotgun at them. Suddenly, the plane flies overhead and drops a bucket down, with a monkey-chain (several monkeys holding hands) holding onto it. The lions get inside and, thanks to some miscommunication up the monkey-chain, the penguins are ordered to take out the dam. Nana stands on top of the dam and tries to shoot the plane down. One of the bullets goes through the plane's windshield and blows off the bobblehead doll's head, prompting Skipper to scream for a medic (Private with duct tape). Out of bullets, Nana waves her hand bag right before the bucket strikes the dam like a wrecking ball, smashing it and sending the water back down the river.
Meanwhile, at the volcano, Julien and Maurice stand alone at the precipice, wondering about the sacrifice. Suddenly, along comes Mort, who survived the fall and the ocean, and is currently trying to survive the huge, scary shark that's followed him inland. As the shark lunges at the lemurs, they hang off the edge of the precipice, while "Jaws" plummets to a fiery death below. At that moment, they see water flowing towards the watering hole, leading them to believe that the sacrifice worked.
The parched animals are overjoyed to see the water back. They are happier when they see Alex and Zuba standing proudly on the capsule, floating on the water towards them. Makunga comes along and haughtily tells them that, though they brought the water back, Alex is still banished. Alex tells him that he has a gift for him and gives him Nana's handbag. Makunga takes it happily, but the gift has an accompaniment inside the capsule - Nana! Seeing her handbag around Makunga's shoulder, she screams "Bad kitty!" and attacks him, leading him away by the ear. Zuba picks up the fallen scepter and offers it to Alex. Alex refuses, but Zuba insists they at least share it, both as Alpha Lions, as the animals rejoice.
We then see the animals at a wedding. Although initially Gloria and Melman are seen in the center of the wedding party, it eventually pans down to the real couple: Skipper the penguin and his hula dancer bobblehead doll, her head held to the body with duct tape. Melman comments off-hand that the marriage will never last. The penguins and monkeys fly off in the plane for a honeymoon in Monte Carlo, to be paid for with the earlier-found diamonds and gold. Alex, Marty, Melman, Gloria and the lemurs are content to wait in Africa until they return. Alex tells Zuba that they'll be staying with them. Zuba has an angry look on his face, but does the "happy face/angry face" gag, with his paw. They all laugh. Melman and Gloria look lovingly at each other, and Melman blubbers about what he loves about her. Marty ribs Alex, saying he's not Marty. Alex laughs, but just for confirmation, looks at Marty's behind for his bite mark again, causing Marty to bump him. They walk towards the watering hole.