How did Nagato gain his Rinnegan?
Well,


   Black Zetsu, which is the embodiment of Madara's willpower and thought (Yin), stated that he had indeed transplanted these eyes into him when he was a boy. When or how isn't stated, one can only speculate that Madara used White Zetsu or some other Zetsu for it.