Was having same problem on one kitkat version of miui.When I unplug headphones, the phone recognizes that headphones are plugged..
Inviato dal mio Redmi Note 2 Prime - MIUI V7 Hermes 6.4.21 EU
Try first to clean headphone jack. Turn device off, blow into jack it could be some dust in jack.
Next thing if doesn't help, there is workaround to avoid this issue, when you are finished with listening music, first exit application then unplug headphones
Sent from my 2014813 using Tapatalk