My doctor said it's because they feel the pain in the gum or have headache due to teething, but they can't distinguish where the pain is, so they just scratch their ears.
Give them some cold stuff to play with so they will bite it. I bought the medicine to put on the gum but people said it's not good, so we rarely use it.