This invariably happens when you are standing up to crank up a short incline. You shift down, and the next thing you hear is a “clunk” as the chain over-shifts past your largest cog and into the space between the cog and the spokes.
As you were putting so much pressure on the cranks at this point, you have undoubtedly pulled the chain further into the spokes than is healthy.
The easy answer is never to shift under pressure. The way to avoid having this happen is to adjust your bike’s shifting properly. The fix is either to grab a handful of chain and pull hard, or better still, to remove the cassette from the freehub and thus free the chain.
This is more involved than it may at first appear. You will have to remove the wheel from the frame in order to get to the cassette lockring, and in order to do this, you may have to undo the derailleur from its hanger (5mm Allen wrench).
With the wheel at least partially removed from the frame (it’s not going far because it is attached by the chain), use the techniques described on the page about loose cassettes to remove the cassette from the rear hub, free the chain, and then reassemble everything.
Before you ride off, check for tight and bent links.