Speaking to Hayden Christensen, George Lucas states that he created Yoda after deciding that Luke needed a physical, embodied mentor after the death of Obi-Wan.
"Unscripted with Hayden Christensen and George Lucas," MovieFoneVideo, September 16, 2012, 6:00-7:14
So when I killed him off, I had to replace him, when I got to the second movie, and I could do certain things by having him come back as a ghost, from the Force, because I had decided to kill him off, in a metaphysical way rather than a physical way, but he couldn't do everything. I needed a physical body to be there to do things, and so I needed something, and I created Yoda, who is two feet tall, large, with green ears, but to be really honest with you, I never really figured out where he came from, what his species is called. It doesn't even have a name. Maybe it's somewhere, but I don't know what it is. So he's a mystery character. He's a magical character. He has no background. He comes and he goes. He's the subversive, secret, mysterious stranger that enters the film and then exits at the end.