What function did The Epic of Gilgamesh serve in ancient Mesopotamia?
The Epic of Gilgamesh is considered the oldest written literature in the world. It predates classical Greek literature by a thousand years, and Christian literature by nearly three thousand. Its verse form reflects the power of oral storytelling and tradition in Mesopotamian culture. Similar to Greek odes, poets most likely memorized the text and recited it as a song-hence the repetitive nature of the text itself.
There is evidence that an actual King Gilgamesh ruled: about 500 years before the epic was committed to tablets. The epic was certainly circulated throughout the ancient world, with copies being found as far away as modern day Palestine and Turkey. Several of the events described, such as the journey to kill Humbaba may reflect the historical Uruk's trade relations, need for natural resources, and later struggles with neighboring city-states over vital resources like wood. The "civilization" of Enkidu may reflect daily rations of the population, such as beer, bread, and oil.
The flood is perhaps the most telling meaning. The culture depended upon the rivers for the rich soil that sustained their agriculture; yet frequent floods also wrecked havoc upon their cities and people. Likewise, Ishtar's Bull of Heaven represents another of the ancient world's great fears: drought, famine, and natural disaster. The ancient Mesopotamians were perpetually caught between the bounty of their river valley and the misery of its floods and droughts.
In these ways, the epic actually revealed the values and struggles of the culture in which it was created. Similar to literature in our culture today (and all other literature at all times), it functioned as a mirror of the society which formed it.
Although the Epic of Gilgamesh has some of the same stories (the flood, for example) as the Old Testament/Hebrew Bible, its main function is not the same in that it is not mainly a religious text.
The true function of the epic is not well known, but some theories have been formed. First, it is believed that the epic would have been a form of oral entertainment, particularly for nobles. Second, there is evidence that the epic was used as a textbook of sorts in schools for scribes. It was a story that they already knew, so it would be easier for them to learn to write. And it had good "moral values" and was therefore worthy of being used.
Please see page 5-6 of the work that I've linked to.