I think it's because the rules of chess don't state that making a move that puts yourself in checkmate results in a loss, they state that you're straight up not allowed to make that move. So if the only moves you have left would put you in checkmate, they're not legal moves.