First off, there are flood narratives all over the world, and they all seem to think that, at one point, every human in the world died except one man, one woman, one man and his wife, or one man and his immediate family. Given the amount of “bottleneck” events in the human genome, it’s very likely that some kind of traumatic flood did happen at some point. And it must have been a doozy for all humanity to still (sorta-kinda) remember it.
As to why it happened, the Bible is pretty clear on that. The Bible writer was obviously concerned to counter the neighboring Middle Eastern peoples’ creation and flood narratives, which said that the gods had just created humans as useful slaves; they got sick of them being yucky and mortal and wanted them dead, which was the reason for making a flood.
According to the Epic of Gilgamesh, all the gods were sworn to secrecy, but one of the gods, Ea, told Ut-Napishtim, the king of Shuppurak. (Well, technically he told the walls of the king’s house, thus not breaking his oath.)
It also said that Ut-Napishtim, the man who survived, was deliberately abandoned his people to die, after getting them to build his boat for him; he pretended that he was just going on religious retreat. The fury of the flood ended after twelve days, because the gods were afraid of the fury of the storm and flood they had raised. At that point, all human beings had not just died, but turned back into clay. Ut-Napishtim sacrificed to the gods, reminding them that without slaves, there was no meat or incense smell for them to eat. Enlil, the boss god, shows up and is enraged that any living thing escaped annihilation, but the other gods object. (And Ea claims that he sent a dream to Ut-Napishtim, and that that was totally not oathbreaking.)
At this point in the story, Enlil turns Ut-Napishtim and his wife into gods, so that there still won’t be any humans left on earth… and that’s as far as we know the story, because we know the story from its recap in the Epic of Gilgamesh, when Gilgamesh asks Ut-Napishtim how he can live forever too.