Yahiko was orphaned during the Second Shinobi World War, forced to steal food in order to survive before teaming up with a fellow orphan named Konan. Soon after the two found a place to call home, Yahiko expressed early displeasure of Konan bringing a another orphan, Nagato, into the group. However, Yahiko eventually accepted Nagato and his dog Chibi into his gang. Angered by how unfair the world was to them, Yahiko declared he would become a god to end the fighting. Soon after the death of Chibi, Yahiko and the others eventually encountered Jiraiya, who looked after them and taught them basic ninjutsu. At one point, Nagato saved Yahiko after an Iwagakure chūnin attacked them. Afterwards, Yahiko vowed to become strong enough to protect Konan, Nagato, and eventually all of Amegakure.
During their time with Jiraiya, he created a defence system so they could protect themselves, should enemy ninja find their hideout. It was comprised of four planks, each with one red side and one white which had a frog picture on it. The four planks were attached to a part of a wall of the hideout with their names underneath each respective plank (Nagato's on the left, Yahiko's in the middle and Konan's on the right while Jiraiya's was on a separate wall). The purpose of the planks was a defence mechanism: if one of them were in the hideout they would simply turn their plank to the red side. If for some reason the red plank was turned yet the person was not there it could alert the others that that member had been kidnapped. If one of them were to leave the hideout they would simply have to flip their plank to the white side. If it should happen that a plank is turned on to the white side yet the person is in the hideout it could mean that an enemy has transformed into said person and has infiltrated the hideout. Jiraiya had also built a trapdoor hidden among the floorboards leading to an hidden room and emergency escape route, just in case.
Yahiko asked why Jiraiya always tried to force his frog ways on them and that they only tolerated it because he was the "Frog Sage". Konan then explained the possible reasons for implementing a code like that which Jiraiya praised her for. He then declared that it was time for training to which Yahiko responded to enthusiastically claiming that one day he would get stronger and change the country before running outside. After hearing his words Konan blushed at him to which Yahiko responded with a perverted smile reminiscent of that of his master's. This also demonstrated his benkei persona where he was a braggart while in his own home, but demonstrated a more reserved demeanour in public. Over time the three would train to be talented shinobi and it is hinted that Yahiko and Konan developed romantic feelings for each other.
The original Akatsuki led by Yahiko.
Some time after Jiraiya's departure from Amegakure, the three gained a reputation as a talented shinobi team. Still desiring to create a world of peace, Yahiko and his friends went on to become fully fledged Ame ninja. Around this time, during the Third Shinobi World War, the trio met Obito Uchiha, who was acting under the guise of Madara Uchiha, and Zetsu. Listening to the results of what would happen from the Eye of the Moon Plan, Yahiko did not trust the masked man, and urged Konan and Nagato to stay away from him. Later, gathering many supporters who shared their ideals, Yahiko established the Akatsuki, though Obito claims to have played a role in the group's creation. Akatsuki spread their beliefs to stop war without violence, and news of their actions would reach Jiraiya from time to time. When their group became too big, they were forced to find another hideout, and leave their current one where they once lived with Jiraiya. When they were about to leave and flip over their "Hop-In" planks, a ninja squad attacked the hideout creating an explosion which resulted in a hole in the roof which they used to infiltrate the hideout. Yahiko (after Konan flipped her plank and just before he was about to flip his), noticed the attack in time and used the escape route just before ninja entered the hideout.
Yahiko entrusts the future to Nagato.
Seeing the Akatsuki's ideals becoming a threat to his regime over Amegakure, Hanzō conspired with Danzō Shimura to eliminate the Akatsuki's three leading ninja. Springing the trap, Hanzō forced Nagato into a position where either the dictator killed Konan in front of them or he murdered Yahiko. However, without hesitation and not wanting Nagato to be put in such a position, Yahiko drove himself into a kunai that Nagato was holding. With his final breath, he told Nagato to continue their mission of finding peace.
Yahiko's death enraged Nagato who used his Rinnegan and summoned the Demonic Statue of the Outer Path in an attempt to destroy the orchestrator of his friend's death with only Hanzō escaping Nagato's wrath. However, crippled by Hanzō with his body rendered emaciated from using the Demonic Statue, Nagato devised a means to regain movement by converting Yahiko's body into a vessel he can remotely control via the chakra receivers on its body as the first of Nagato's Six Paths of Pain: the Deva Path. Due to his friendship with Yahiko in life, thus most recognising him as Pain while only Konan and Tobi know the truth of his method, Nagato uses the Deva Path as his primary body in all Akatsuki matters. Even when all Six Paths were in active use, Nagato tended to speak only through the Deva Path and even sacrifice the other Paths to ensure no harm comes to the Deva Path.