Yankee doodle was a sound made up by the British during the revolutionary war. Macaroni was another word for fancy. So the joke was the American were so dumb that they would put a feather and call it fancy. But the American liked the tune so they changed some words and then sang it so that the joke was on the British.
Thank you Deathstriding