If you suspect TMJ dysfunction, i would first recommend a full exam by a dentist with experience in treating tmd/tmj.
He can tell you if your TMJ is part of the problem including the cause of the head pains. Another possibility from your symptoms could be a sinus issue. When a pain in your head occurs and goes away due to head position, that can involve the sinuses. Good luck.