The shinobi originated from a continent ruled by them. The three major villages: Water, Lightning, and Flame were all ninja villages, were feared by all the less prominent ones. Other than them, there were two other known villages, known as the major-minor villages, which weren't on the level as the three major ones, but were still more prominent and powerful compared to the rest in the world. They were the Earth Village and Wind Village. The Earth Village were a people who rejected the usage of chakra and focused on the innovation of technology, while the Wind Village were a village comprised completely of monks around the world that came together to worship the Sage of Life's son, but adopted the shinobi way to increase their success.
Once upon a time, there were samurai villages, but with the creator of them, Kyūkyoku, being dead, and not able to defend them, the shinobi wiped every single last one of them out. But one day - a day remembered by the surviving shinobi to this day - four samurai showed up to each of the major villages going under the name, "Odotai" (lit. Zodiac). And upon their arrival, they slayed every less ninja that they saw. Even the strongest ninja of the villages together couldn't stop these samurai, and were slain with the weaker ninja.
Noticing the assured victory of the Odotai, Pirates of Noumu Harbor, a harbor of the Water Village saw this as a perfect opportunity to take advantage of people in their most vulnerable time. They sailed across all the borders of the continent, offering ninja transportation to "a new land," as they called it, in exchange for their most valuable items. Ninja in fear of course took them up on their offer, viewing their items as worth than their life. This trade was done multiple times, and with multiple ships.
Ship by ship, ninja were brought over to this "new land." Upon reaching the land, they would come to a jungle terrain. The ninja would discover a group of samurai there. They were scared at first, but after conversation they noticed these samurai were different, and non-harmful, at least now. The ninja being in a foreign place, felt it would be a good idea to be apart of someone else's village, so they asked these samurai to introduce them to their leader. The leader was a serious and intimidating man, so the ninja approached him with respect when asking to join his village. The leader said he understood that Taiyō no Kuni - the jungle could be a very dangerous place, and so with open arms he let the ninja join his village, Yorugakure (Village Hidden in the Night). But these samurai were not ignorant of ninja and their hands were on their hilt just in case any betrayal.
Unfortunately, Yorugakure could not hold all of the ninja, so the samurai informed them of a village in the desert known as Higakure (Village Hidden in the Day). At sunset, after waiting around the oasis that connects the desert and the jungle, the ninja set out to find this village, and upon seeing the opening of the village that the samurai notified the ninja about, the ninja entered. The village appeared to be dead and the ninja weren't sure if this was actually Higakure. But as the sunset and the moon rose, footsteps and voices start to be heard and doors start to be open. The ninja did not want to be seen as a threat and made it clear to the citizens that they just wanted to speak to the leader.
The citizens obliged, taking the home-seeking ninja to their leader. The leader was a nice man and had no problem meeting the ninja. After the two introduced themselves, it was discovered the leader had never even heard of a ninja. The ninja broke down who they were and what they do and the leader with a smile welcomed them to Higakure on the condition that they help fend off the many bandits of Tsuki no Kuni (Land of the Moon).
With their home continent covered in blood, these shinobi sought to begin a new life here, and make the best of it, for new generations.