Tablebase-perfect positions slip from win to draw, or draw to loss, in the last handful of pieces.
With few pieces left, tiny things decide the game: the opposition of the kings, an outside passed pawn, whose king is more active. You went wrong in the technique, not the calculation. In the endgame your king becomes a strong piece — march it forward; push passed pawns; and learn the handful of basic won/drawn setups, because they recur in game after game.
The good news: it is a pattern, not bad luck — and patterns can be trained. Step one is measuring yours: how often it appears in your real games and how many rating points it drains. Then the daily repair: 5 minutes on your own positions, not invented puzzles.