Black Zetsu is the answer to everything. Just inhale a piece of him and you'll be granted visions of what he has seen.
When Madara was asleep, BZ stuffed a piece of himself up Madara's nose and into his brain. Madara then "dreamt" of a boy named Obito. One day, SZ came out of the Mazou and watched Madara poop. He became obsessed with poop after he witnessed that incident. Perhaps he was traumatized and wanted to make sense of what was happening.
It was because of that obsession that he eventually found Obito. He told Madara and he instantly remembered his dream. He used Limbo to let his shadows travel and gained their knowledge once they returned to his body. He was on life support so he couldn't leave the cave, or so he thought. He took the giant statue with him and snuck into Nagato's home while they slept. The Gedo broke the roof and killed Nagato's brother, who wasn't mentioned in the manga.
Madara had SZ transplant his eyes onto Nagato while Nagato slept. Madara had a napkin over Nagato's nose that contained a substance preventing Nagato from waking up. He left after the transplant was complete. He traveled to The Mist Village to talk to the Mizukage (he was an Uchiha but the manga never mentioned this). Everyone was asleep cuz night. He instructed the Uchiha to do the things that eventually led to everything up until the Obito massacre Mist Ninja part (Too lazy to go into detail of what REALLY happened behind the scenes.)
Oh right, and then BZ traveled from the Mazou to Madara, and from Madara to the White Zetsu. Somehow.
Something like that.