I found a method that worked for me. If you're having a problem similar to mine and cleaning the dock did NOT work for you, try this at your own risk. Disclaimer : This method might permanently damage your iPhone, mine is out of warranty so it wasn't a big deal if I damaged it, I would still need to pay for replacement.
I don't know what may have caused the phone to lock at the
docked state, but my first thought is that there is a switch mechanism inside the dock connector. All I did was get a key and jam it there, and rotate it a bit until it got fixed.
Note: If I inserted the key there and did not rotate it, the problem is fixed temporarily until the key is removed. The only way I got rid of the problem completely is to rotate the key and push the pins downward.
If you try to insert the key and remove it, the problem still persists. I had to jam the key in deep and rotate it a few times (gently) to permanently fix this issue.
Make sure you insert the key in the
middle - left part of the dock connector, that's where pin 11 is located (I think).
After placing my phone on a dock again, I lost control over the sound. I did the same method with a bit more rotations using the key and now the problem is gone 100%. Not sure how, not sure why, but it worked.