This was written by Paul McCartney for the 1969 film The Magic Christian. McCartney made a demo of it in July 1969, playing all the instruments and singing the double-tracked vocals.
When Badfinger (who until then were called the Iveys) went into the studio the following month, McCartney produced the song, telling them to copy his version exactly. In early 1970 it got to No 7 in the States and No 4 in Britain.
This is an interesting video about the group.