If we don't know what the first was, what's the first we know of?
This question is a very tough one to answer because early literature often defies classification. There are a lot of reasons for this - the blending of history, myth, and fiction; the barriers presented by ancient styles and languages (for example, because ancient Egyptian, the language in which the earliest literature we know of was written, had no punctuation, no vowels, no breaks between words and no line breaks except where necessitated by the width of whatever was being written on, it's very difficult to discern prose from poetry because it's almost impossible to tell whether rhythm or metre is present), and the fact that ancients operated with very of few of the literary conventions we use to classify work today.
The case can be and has been made for the very earliest Egyptian and Mesopotamian literature to be considered as early plays. They were often very oratory and rhetorical pieces intended to be performed aloud, in many ways like a one man show is done today. Again, classification remains difficult, but the editorial board of the journal The Literature of Ancient Sumer speak to this a bit: "Nowadays we think of literature as comprising poems, novels, and plays. Something resembling all three can be found within Sumerian literature, often within the same composition."
If we were to accept this claim and classify ancient Egyptian and Mesopotamian literature as plays, or at least having playlike aspects, I think the strongest example is "The Tale of the Eloquent Peasant," which is one of four surviving works we have from Egypt's Middle Kingdom, meaning they were produced somewhere between 1990 and 1800 BCE. It's about a peasant making a case to his king to receive justice over the course of nine monologues. In The Novel: an Alternative History, Beginnings to 1600, Steven Moore describes the story as such: "[it] consists of nine petitions by the eloquent peasant on the nature of justice, each employing extended metaphors and rhetorical fire works ... The Egyptians enjoyed didactic tales and instructional literature, but valued rhetorical skill above all." It's possible that the existing document even functions like later Mesopotamian texts, of which Moore later writes (quoting Stephanie Dalley within), "we should consider the surviving texts 'as bare skeletons which were fleshed out in practice by skilled narrators, rather as early musical notation gave only the guidelines needed to remind the musician of appropriate melody and rhythms, leaving embellishments and flourishes to his own skill and popular taste' - like jazz musicians today. That is, what may look like poetry may have become prose in performance". This makes me think of two things: the first is Shakespeare in performance, in which the iambic pentameter may only be detectable to the carefully listening ear or even abandoned by the performer altogether. The second is improv, which is essentially what Moore is describing - starting with a bare-bones direction for plot and important beats and allowing the performers to fill in the rest.
As far as your other questions, the purpose of a lot of these performative pieces was essentially entertainment of the king and/or other nobility, with, as always, the caveat that it's difficult to distinguish history, myth, and fiction in a lot of cases in this era, so each piece may also have held ritual, religious, and/or historical value as well.
If, however, we reject Moore's reading and choose to view ancient literature in a more conventional sense, then the short answer is that we don't know. Aristotle tells us (writing c. 335 BCE) that theatre and drama as we would know and recognize it today developed in Athens out of festivals meant to honour Dionysus - I personally speculate that these festivals probably featured oratory performance pieces similar to those described above, but have zilch as far as evidence for that. We don't know what the earliest plays are, because the earliest work we have - the plays of Sophocles, Euripides, Aeschylus, and Aristophanes, writing in the 5th and 4th centuries BCE - is only a small portion of what those four playwrights wrote and only a fraction of the hundreds or even thousands of plays that were being produced at that time, per Oscar Brockett and Franklin Hildy's History of the Theatre.
The oldest of these playwrights was Aeschylus, and his earliest play that we can date is The Persians, produced c.472 BCE. It was a tragic retelling of the Battle of Salamis, meaning its purpose would have been entertainment, catharsis, and, to an extent, historical posterity. It would likely be very similar in many aspects to theatre as it's performed today - many of the plays by the playwrights mentioned above are still performed today in translation. This was likely not Aeschylus' first play, though - check out the Lost Works section of his wiki article for a full list of over 70 other plays attributed to him that we have know knowledge of beyond their titles.
I should also mention Thespis, the actor and possibly playwright (we know the titles of a few plays which have been credited to him, though the actual authorship is unknown and contested). He is credited by Aristotle (by way of Themistius, and beware the one who tells you otherwise - to my knowledge, Aristotle never referenced Thespis by name in his own writing) for having been the first person ever to appear in character in a play. If that's true, we can essentially consider Thespis the inventor of modern theatre, dating the earliest play in the modern understanding to sometime during Thespis' life in the 6th century BCE. Aristotle describes what is generally agreed to be Thespis' first performance as spinning out of a dithyramb, a sort of choral performance piece, but that explanation has come under criticism. Given the uncertainty about the circumstances of Thespis' first performance, it's virtually impossible to say what the plays he performed in might have been about, what purpose they served, and how they compared even to the plays of the four 5th century playwrights, let alone modern entertainment. However, following the traditional explanations, he assumed the role of Dionysus responding to the dithyramb, adding onto what was a ritual performance intended to honour and celebrate Dionysus that may have had more in common with a Mass service than plays as they're performed today.
Sources:
Steven Moore, The Novel: an Alternative History, Beginnings to 1600 N.B. that Moore is a literary critic, not a historian, and there are several times in this book that he misrepresents history and a couple where he simply gets it wrong. That said, these incidents are almost never in regards to the literature itself. He is a thorough academic and has obviously done his research, and relies on the research of reputable scholars who are experts in their fields when he is no 100% confident in his own.
Oscar Brockett and Franklin Hildy, History of the Theatre
Aristotle, Poetics. His particular comments about early Greek theatre and the three tragedians of the four playwrights above (Aristophanes was a comedy writer) can be found in Part IV. Poetics is readily found online in its entirety, as is Rhetoric in which he also writes a bit about actors and the early theatre in Book 3.
Gerald Else, "The Case of the Third Actor," Transactions and Proceedings of the American Philological Association, volume 76. A link if you have JSTOR access