I was thinking about this the other day: although modern fantasy/RPG lore often have 'mad gods' ("mad" in the "insane" sense), I can't think of an ancient religion or mythology where a god is portrayed as being truly insane.
Given the idea that these belief systems are often created to make sense of the world, saying, "Well, actually it's just because this God is total nutso" seems like it would defeat the purpose.
Of course, I know that many mythologies have mischievous, capricious, and unpredictable beings or evil gods of chaos, but how many have gods that are considered to literally be crazy?
I think the major issue that comes with answering this question is that the idea of "insanity" and "sanity" are really very modern concepts that do not translate into ancient religions in any capacity. While certainly there would have been people who we today would classify as mentally insane in the ancient world, the people of that time would have understand the condition that the person was experiencing in a different way. In Mesopotamia, the area that I study, mental illness was often associated with an individual being "touched" by the "hand" of a specific god, or by a specific malicious entity of which there were very many in Mesopotamian beliefs. What we refer to as "witchcraft," in Akkadian "kaššaputu", would also be something that could cause the conditions that today we would consider to be mental insanity. As such it is difficult to define what people in the ancient world would have viewed as being "insane." What actions constitute insanity? That question is important to keep in mind as we look at the idea of insanity in Mesopotamia and gods that do things that may be considered somewhat "insane".
There are varying opinions in the study of medicine in the ancient world as to whether we should even be talking about illness, mental insanity being one of them, with the same terminology that we do today. It is often quite popular to do what we call "retrospective diagnosis," which attempts to diagnose modern diseases in ancient times. Many historians of science studying ancient medicine in recent years have pushed back significantly against the idea of retrospective diagnosis because the experience of an illness is often what is most important. Certainly, we may be able to look at the symptoms reported in a cuneiform tablet discussing diseases and claim they are the same symptoms as such and such disease, but remember that the people of the time didn't necessarily look for the same symptoms in the same combinations as we do today. What we could identify as symptoms of a single disease today may have been referred to as three different diseases based on symptoms in the ancient world, and what we may recognize as three different diseases with similar symptoms today may have all been called the same thing.
As such, it is very difficult to translate the concept of mental insanity back into the ancient world, which would make it difficult to really argue that they viewed any of their gods as "insane." I think this also hits at the nature of deities as a whole in the Ancient World who often behaved in ways that we today do not really understand. Personally, I like to view ancient deities as foils for their society and so I think many of the things that they were experiencing in their own lives appear in the actions of the gods, and so often gods will do things that really do not appear to be god-like. In the old Sumerian story about Inanna and the "me"s, basically the powers of the gods, Inanna gets the god Enki very drunk and then steals all the "me"s when he's asleep to bring them to her city Unug (later Uruk). It is curious to think that they believed the goddess Inanna had to steal these powers (which amounted to all of civilized life in a way, from their description) from another god, and that the god Enki could be so easily tricked into getting drunk enough to lose sight of his "me"s. That being said, these are the gods and so ancient peoples likely believed that while they may do seemingly imperfect things that does not detract necessarily from their perfection.
If we want to define the idea of a "mad god" as a god that goes against the will of the other gods, there are certainly plenty of examples of that in the myths of Mesopotamia. The concept of the "angry god" is very common in Near Eastern mythology in general and is a basis for the flood story in very nearly every version. The story of Atrahasis (see where I got my username) is a very good example of this. At the start of the story, the world is created and then the Annunaki are created. The Annunaki then create the Igigi gods to do their labor for them, but this becomes an issue when the Igigi revolt against their overlords. The Annunaki decide that to quell the rebellion they will create humans to do their work by sacrificing one of the Igigi gods. After a time, the god Enlil becomes enraged with the "noise" of humans and so he decides that he is going to destroy all of humanity with a single action. Here we can see this sort of trope of a "mad god" in effect somewhat, and there is also the classic "trickster" god Ea who subverts Enlil's attempts to destroy humanity each time by warning the principal character Atrahasis about the coming catastrophe and advising him how to stop it. After being thwarted multiple times, Enlil resolves to bring down a massive flood and forces all the other gods to swear they will not tell the humans, but Ea again subverts Enlil by talking to Atrahasis through a wall. Atrahasis builds a boat and manages to survive the flood because of this.
What I think is most interesting about the depiction of the gods in Atrahasis is how they are described after the flood. When the flood subsides, Atrahasis lays out offerings of food for the gods and the text literally states that the gods swarmed to the offerings "like flies". The gods realized with the flood that they were dependent on humans to provide for them, and so they swore together that they would never attempt to destroy humanity again. I am not entirely sure whether you could call Enlil in the story of Atrahasis "mad" but the concept of one god becoming enraged and taking an action that eventually the other gods realize was a mistake is a core theme of the flood myths in Mesopotamia, and it appears again in the Babylonian creation myth Enuma Elish. There Tiamat, the sea, is the enraged god and Marduk attains his position as king of the gods by slaying her. The "enraged" or "mad" god changes depending on culture but it is certainly a concept that existed in the ancient world.
Sources:
Foster, Benjamin. 2005. Before the Muses: An Anthology of Akkadian Literature. Bethesda: CDL Press.
Leven, K. H. 2004. "'At times these ancient facts seem to lie before me like a patient on a hospital bed' - Retrospective Diagnosis and Ancient Medical History". In Magic and Rationality in Ancient Near Eastern and Graeco-Roman Medicine, edited by Manfred Horstmanshoff and Martin Stol, 369-386. Leiden: Brill.