Hi, just had this exact same issue happening over the last week or so - after some searching around on threads one of them suggested testing it in the diagnostics:
Menu > Settings > About phone > Diagnostics > Test Device > Tap Ear Proximity
I did that and it suggested my proximity sensor wasn't working at all.
Another thread suggested pushing the top of the screen - and that fixes my sensor temporarily, so you could try that as a temporary fix when needed?
On closer inspection I found that my screen is actually lifting from the body of the phone a little at the top right, so that's causing the fault. When pushed closed the sensor works. I've also got yellow tints on the top corners of the screen which I've learnt is apparently due to the glue that holds the screen failing/melting.
It's really disappointing since I had the same issue with a Z3 compact where my whole front screen eventually came loose.. so it seems like Sony didn't bother to fix it for the Z5 then.
Not sure how to get this fixed properly though unfortunately.