Numbers 22:27-31. When the donkey saw the angel of the Lord, she lay down under Balaam. And Balaam’s anger was kindled, and he struck the donkey with his staff. 28 Then the Lord opened the mouth of the donkey, and she said to Balaam, “What have I done to you, that you have struck me these three times?” 29 And Balaam said to the donkey, “Because you have made a fool of me. I wish I had a sword in my hand, for then I would kill you.” 30 And the donkey said to Balaam, “Am I not your donkey, on which you have ridden all your life long to this day? Is it my habit to treat you this way?” And he said, “No.” 31 Then the Lord opened the eyes of Balaam, and he saw the angel of the Lord standing in the way, with his drawn sword in his hand. And he bowed down and fell on his face.
There is only one other place in the Bible where an animals speaks and that is the snake in the garden of Eden (Genesis 3:1-4). It seems that on these two occasions, God miraculously caused the animal to speak in order to fulfil his purpose.