Yahiko was born in the Rain village and became orphaned when a war broke out between three countries that used the Rain village as a battleground. He met Konan and the two managed to survive together, they eventually ran into Nagato whom they invited into their fold.
Yahiko was a strong-willed individual that hated how the it rained so much, believing that it was like the whole village was crying and made it look weak. He also hated war and violence, and wanted to become a god and make everyone feel his pain. But he needed to become strong first, and wanted to learn ninjutsu. They eventually observed a fight between the Sannin and Hanzo the Salamander. Witnessing how powerful they were to stand up to the renowned Hanzo, Yahiko wanted to use them to further his goals. Jiraiya would take mercy on the orphans and take care of them for three years while he trained them. Yahiko took a profound liking to Jiraiya and came to live by his teachings.
After Jiraiya left, the orphans started trying to make the Rain village a better place. Being motivated by an unknown benefactor, Yahiko formed a revolutionary group that became famous and grew very quickly, believing in the ideals of peace that Yahiko spread. During their time, he and Konan fell in love, and he convinced Nagato that he would be the world's messiah. Eventually they gained the attention of the Rain village's leader, Hanzo, whom wanted to use them to try to negotiate a peace treaty between the warring countries. This however was only a half truth, as Hanzo feared Yahiko's influence and Danzo was frustrated by their involvement in the war, the plan was to kill them in order to form and alliance with the Leaf village. With Konan's life in danger, Yahiko gladly sacrificed himself.
After his death, Nagato snapped and went back on Jiraiya's teachings, favoring Yahiko's childhood goal of bringing pain to the world. Nagato reanimated Yahiko's corpse and pretended as if he were still alive, turning him into a "god" as he originally dreamed and had him "lead" Akatsuki.