Can someone explain why should white lose his queen in the following tactics? This is my initial position:



I exchanged my pawn to get queen at this position:



Now to complete the tactics, the engine is accepting only this move which results in a stalemate



I would have dominated black with my queen and bishop but the tactics did not allow to do so which implies losing queen is better. But I don't understand why.

I am trying to understand why is Q5f the best move for white.
At this position you pretty much lost already - except if you can save yourself into a stalemate. That is exactly what is happening in your example. Qf5 forces black to make a decision between one of the following choices: He can either move his king or his pawn, or he can take your queen.

If he chooses to move the king out of check or block the check with his pawn, white is free to take the black queen. While it is not an easy victory, playing with a queen against two bishops and a pawn can still be won.

If however black chooses to take white's queen, then white will be trapped in a stalemate, resulting in a draw. Getting half a point is better for white than losing (and getting no points). Given the imbalance of the material, there is also no way that white can win this endgame (against a reasonable well playing black player).

Explanation for the stalemate: After the white queen is taken, white only has his king left. But the king can't move anymore. The square d4 and d2 are controlled by black's (black) bishop. d3 and e2 are controlled by the black bishop on b5 and e4, d3 (again), f4, f3, f2 are all under the control of black's queen.