It's still unclear:
It’s unclear exactly why [parrots] evolved this capability [for vocal learning], but research suggests they use it to tell members of their group apart from members of rival groups. … According to this theory, birds that are raised in captivity might mimic their human owners as a way of gaining acceptance as a member of the family. If they hear “pretty bird” a lot, they’ll interpret that as a call distinct to their flock, and try making it themselves. If the parrot gets no response when she squawks, but gets lots of attention or treats when she makes humanlike noises, she has an extra incentive to practice forming words and phrases.
Whether parrots can ever understand what they’re saying is a matter of debate. Researcher Irene Pepperberg trained an African Grey Parrot named Alex to speak with a vocabulary of some 100 human words. Over time, he learned numbers and phrases as well, and when shown a toy, he could correctly identify its name, color, and shape. Skeptics have suggested that Alex’s abilities might have been a product of the “Clever Hans” effect, in which an animal gives correct responses based on its trainer’s body language, as opposed to genuinely understanding the question.