This is a legal move known as “en passant”. The Wikipedia article gives a good introduction.
It is described in section 3.7 (d) of the official rules (PDF):
“A pawn occupying a square on the same rank as and on an adjacent file to an opponent’s pawn which has just advanced two squares in one move from its original square may capture this opponent’s pawn as though the latter had been moved only one square. This capture is only legal on the move following this advance and is called an ‘en passant’ capture.”
See the Lichess training on this move for some practice with it.