Andrew Jackson's detractors in his 1828 presidential campaign creatively played on his surname by referring to him as a 'jackass', and therefore made a literal ass (or donkey) his symbol. It didn't help that he was known for being stubborn, a trait perceived as common in donkeys, so public perception came to continue associating him with that animal during his presidency. Rather than transferring to the Democratic party in general, this symbol stuck with political cartoonists first, particularly Thomas Nast in the 1870s.
And it would be Thomas Nast's cartoons that popularised associating the elephant with Republicans. One particularly famous cartoon of his depicts the Democratic donkey in a lion costume scaring away a 'timid' elephant, labelled in the drawing as 'the Republican vote'. This was well-received enough for the Republican party to also end up stuck with an animal mascot they hadn't picked themselves.
Hi! This topic could use more info on 'why the elephant image?', but you'll find info on the donkey in these