There are many examples of humor in To Kill a Mockingbird, and author Harper Lee often uses it to break up the more serious scenes of the novel. In Chapter 8, she uses the comic description of Mr. Avery's peeing exhibition just before the children make their raid on the Radley's back porch, resulting in a shotgun blast from Mr. Radley. She follows this serious and potentially deadly action with another comic scene--that of Jem standing in his underwear while Dill tries to explain that they were playing "strip poker" without cards. During Tom Robinson's testimony, in which the prosecutor repeatedly refers to Tom as "boy," Dill becomes sick to his stomach. Lee inserts the humorous scene of Scout's and Dill's meeting with Dolphus Raymond to ease the tension. When Raymond's cries about how "the hell white people give colored folks" becomes too much for them, the children retreat back to the courthouse. Lee uses this strategy once again in Chapter 28, building a creepy atmosphere as the children walk to the school before giving the reader relief when Cecil Jacobs jumps out of the darkness to scare them. Then comes the comic relief of the Halloween pageant, in which Scout wears the ridiculous ham costume and then falls asleep and misses her entrance.
They told me later that Judge Taylor went out behind the auditorium and stood there slapping his knees so hard Mrs. Taylor brought him a glass of water and one of his pills. (Chapter 28)
It was the author's way of controlling the readers' emotions by taking them from the lows of one humorous situation to the highs of the much more serious event that was to follow: Bob Ewell's attempted murder of the two children--and Boo Radley's unexpected appearance in which he saved their lives.
I believe that the Halloween pageant was added to add humour (Harper Lee is a humourous person). However, the more important reason is because with the costume on, Scout couldn't see anything happen (Bob Ewell trying to kill Scout and Jem, Boo Radley saving them) and thus making the reader not know anything, which adds to the emotions (perhaps shock) of knowing later that Boo Radley, the "mysterious and evil man of Maycomb that supposedly "kept dead animals in his house and scratches screens and is insane" would save them from a murder by the hands of Bob.
Hope this helped. :D