The main reason why it exists in the narrative is that it precedes the epic by quite a long time. Before the epic of Gilgamesh was put together as one single narrative, hymns of Gilgamesh were performed separately. These are the Sumerian predecessors of what's commonly read today. They can fortunately for us be easily found on the internet at The Electronic Text Corpus of Sumerian Literature (ETCSL).
As you can see, there were several hymns in the Gilgamesh cycle, and the bull of Heaven is one of them. All of these except Gilgamesh and Aga were eventually incorporated into a single narrative. This seems to have happened first ca. 2000-1600 BCE. What's often translated and put into books, however, is a recension of this text made roughly a thousand years later. By that time, it would be too late to remove it.
Historically, the scene has to be rather late overall. The original story was probably the hymn of Gilgamesh and Aga. Aga (also Akka) in the myths was the son of Enmebaragesi, king of Kish, whom we know now was actually a real person from inscriptions1. A plausible scenario for the development of Gilgamesh follows:
Enmebaragesi of Kish ruled over Sumer, and was succeeded by Aga, who was then conquered by Gilgamesh of Uruk, leading to Uruk's predominance.
This could have (and would have) been ritually performed at Uruk, where Gilgamesh could then fade into legends. Furthermore, other myths would be attracted to it. First, the Humbaba quest likely originated with the Ur III dynasty.2 Originally entirely separate from the Gilgamesh cycle, the story of Dumuzi and Inanna is well chronicled in the Sumerian literature. You can also read them at the ETCSL. The older view, established by Frazer, was to see Dumuzi was a dying and rising fertility god, but as JoAnn Scurlock points out, the evidence for that is very weak.3 Instead, he appears to:
embrace [the onerous duties of kingship], and to suffer the ultimate penalty for failure.
If this reading is correct, it explains why Dumuzi, lover of Inanna (later Ishtar) became the predecessor of Gilgamesh (and defeater of Enmebaragesi), itself evidenced by the unusual name appearing as a doublet (he's distinguished in the sources, but it's clear it was one name that became two).5 Dumuzi would be the example of a king failing at his duties, while Gilgamesh would have been a good king (the part about Gilgamesh being a bad king in the beginning is missing in the Sumerian hymns and is presumably a later addition).
Ritually, it appears that there was a differentiation of some sort between Dumuzi and Gilgamesh. The Inanna/Dumuzi relationship looks similar to other goddess/dead mortal ones. Beyond them, we have Adonis and Aphrodite (Adonis actually comes from the Semitic root adon which means "lord", and was a title for Tammuz, the Babylonian version of Dumuzi; Aphrodite has similar myths to Inanna/Ishtar, e.g. she's insulted by Diomedes in the Iliad and reacts suspiciously similar to Ishtar when she's insulted by Gilgamesh, i.e. going up to heaven and crying to her parents; Homer, Iliad, 5.297–430), Cybele and Attis, and perhaps too Aphrodite and Paris.
These couples mean something—though what exactly it's difficult to say—about the function of kingship in ancient Sumer and Babylon. The king was also the chief priest, and there seems to be some sort of relationship between the king and the goddess, as you can see from my post on Shulgi and Inanna.
At this point we can only speculate, but perhaps Gilgamesh's rejection of her is tied to a religious dispute or a political dispute? If there is any truth about Gilgamesh's involvement in the building of the temple of Ninlil as recorded in the Tummal inscription, then there might have been rivalry between worshipers of Ninlil and Inanna that played out in Gilgamesh's favoring of the former over the latter. It's difficult to say for sure, and of course, all this is not only hypothetical, but also likely to remain unanswerable.
(Interestingly, Homer in the Iliadic parallel, used it for polemic against the claims of Aphrodite as a warrior goddess, since Zeus chides her for trying to engage in combat. We know that in Cyprus, where there was a strong Phoenician population, Aphrodite was dressed in warrior garb. I think there's something there.)
(Aphrodite in warrior garb. Mosaic, found in Paphos, Cyprus)
Narratologically, the inclusion of the scene makes sense for the overall story, and in fact is one of the more important parts. One of the important themes of the work as a whole is the acceptance of death for mortals. Recall that the original impetus for the slaying of Humbaba is for eternal glory: "I will establish for ever a name eternal!" (Y 187, trans. Andrew George). In order for Gilgamesh to really seek out immortality (almost always denied to mortals, with Ziusudra being the one exception), he must gain a reason for wanting it. This is accomplished in the death of his friend, Enkidu. And who else, except Inanna, who already was responsible for the death of Dumuzi, would be implicated? The association between the two would be even stronger since there are hints of immortality in the Dumuzi hymns.
Thus this scene provides that pivotal climax which leads him to seek out immortality and ultimately the acceptance of his fate, since it is Enkidu from the Netherworld he sees.
Jean-Jacques Glassner and Benhamin R. Foster (ed.), Mesopotamian Chronicles, Brill: 2005.
Dina Katz, Gilgamesh and Akka, Brill: 1993.
JoAnn Scurlock, "Images of Tammuz: The Intersection of Death, Divinity, and Royal Authority in Ancient Mesopotamia," pp. 151–182 in Experiencing Power, Generating Authority: Cosmos, Politics, and the Ideology of Kingship in Ancient Egypt and Mesopotamia, ed. by Jane A. Hill et al. University of Pennsylvania Press: 2013.
Jeffrey H. Tigay, The Evolution of the Gilgamesh Epic, University of Pennsylvania Press: 1982.