I noticed that too. And it's not only the accent but the usage of words (different for lawyer and for a farmer), the sounds the person is making, movements and gestures.
I think it has something to do with "calibration" of your communication with your "opponent". to get the best possible results when the brain thinks it's possible.
Notice how you do the same with small children. I think it would be very weird if you try to talk with a toddler like: "Dear Mr. John Smith, I would like to ask you if you like this yellow toy made of plastic."
I am sure you would say something like "Johnny, do you like this yellow doggie?"
By calibrating I mean, that if you know the background, accent, social status you adapt, your brain makes some calculations and decide what is the most beneficial behaviour (yeah, selfish gene again ;) ) based on your experience etc.
In this case to make the communication the least problematic you try to sound and act a little like your opponent.
When you really don't like the person you will not sound like him. But be aware that if it's e.g. some guy that was popular in the school when you was younger you can pick his accent too ;). It's because even if you don't like him, parts of your brain sees him as somebody who was more successful, had more girls etc. and your body reacts to your hidden/deeper thoughts and memories and try to impress him by sounding/acting more like him.
Another important thing in this process is that sounding like the baby (high pitched voice), like a farmer/football fan (deep voice) transform you a little closer to your opponent. So you try to mirror him. And everybody is a little narcissist and like when he heard his voice (rewinding to your part on a record if you have a featuring as a singer) or see his own picture (the first thing after finding a picture is finding yourself) etc. ;)
Sometimes you imitate people when you don't like the person, but then it's intentional and not unconscious (as you mentioned in your question) and you try to make fun of or annoy that person etc.