The starting point is that it's useful to distinguish between "text I wrote" and "text people wrote to me," and color is a simple way to clearly make that distinction. (It's not the only way, certainly -- the left/right alignment indicates the same thing -- but the color distinction certainly makes the difference very clear indeed.)
As for the choice of where to put the color and where not to: Hold your phone some distance from your face, or take off your glasses if you wear them, and see which text is more prominent / easier to read.
For a received message, it's important that the text be as readable as possible, so it's displayed in a high contrast black-on-gray. Your eyes are drawn to the text itself.
For sent messages, it's less important that the actual text be readable, because you presumably already know what you wrote. Therefore a less-readable white text on colored background is acceptable: your eyes are drawn to the message bubble itself ("I said something at this point") rather than the text contained within it ("I said this specific thing").
(There's also going to be a certain amount of 'the colorblind need to be able to see the difference', 'it just looks blah without some color', and 'similar existing apps do this so we will too' mixed into the equation, of course.)