So, on September 8th, 1967, they met at Abbey Road Studios and laid down the basic track for a tune called Aerial Tour Instrumental. It would become the first song credited to all four Beatles, though Paul McCartney claims that he wrote the simple theme. The basic track had Paul, George and Ringo on their usual instruments and John playing chords on an organ. By take six, they had arrived at the best, so overdubbing began. George added a mellow countermelody on guitar during the second half of each verse as John played Paul's simple theme on the Mellotron for the second and third verses. The third verse of this instrumental also incongruously included the voices of all four Beatles singing the melody in unison as a simple "la la la." According to engineer Geoff Emerick in his book Here, There and Everywhere, it was Paul's idea to have Ringo's voice most prominent in the mix, thus sounding unlike any previous blend of the Beatles' voices.
All of this came in at a little under two minutes, but a jazz saxophone recording found among the Mellotron's many sound effects was added, as well as several minutes of doodling on the Mellotron, some of it recorded backwards. The group returned to the track on September 28th, adding a bit more Mellotron, guitar and maracas. John and Ringo then overdubbed a number of tape loops over the swirling Mellotron section of the piece, stretching it out to somewhere between nine and eleven minutes in length.
Once it came to the mono mixing stage, however, the song was cut back to just the three verses and a tiny fraction of the effects section, omitting the saxophone tape entirely and coming in at a modest 2'16". By the time that the stereo mix was done in November, the song was now known by the title Flying.
It is played early on in the film as the patrons look out the right side of the bus and the countryside changes colors in classic psychedelic fashion. When the BBC broadcast the film in black and white on Boxing Day of 1967, this sequence was, of course, quite unremarkable to the audience. Some of the film used, by the way, came from none other than Stanley Kubrick. Magical Mystery Tour producer Denis O'Dell somehow got the legendary director to give him footage that was shot for either Dr. Strangelove or 2001: A Space Odyssey according to various sources.
The original double-EP of Magical Mystery Tour released in the UK credits the song to Harrison/Lennon/McCartney/Starkey. My copy of the American LP lists the credit as Lennon/McCartney/Harrison/Starr.