This will only happen when you are sprint swimming underwater when you use a fully charged attack. If you are sprint swimming without going underwater (in "diving" mode) or spam-clicking while attacking a mob underwater, this will not work. It is really annoying and probably a bug because you can sprint attack without sprint mode stopping on land.
Linked issues
is duplicated by
relates to
Attachments
Comments

According to MC-168204, this is caused by a client-server desync.

Confirmed for 1.16 pre-release 3
This is also happening on land. Should I make a new report, or add the information I found to this report?
Can confirm in 21w07a.
Can confirm in 1.17.1. I've attached a video demonstrating this behavior.
[media]Can confirm in 1.19.2. This behavior is the result of desynchronization between the client and the server. This ticket closely relates to MC-69459.

Video from my report that duplicated this one. This is a very annoying issue as the server thinks your hitbox is standing upright, causing you to get stuck on ceilings and can't swim through one-tall gaps.

Works as intended, sprinting/swimming are stopped when an entity is hit, and a sprint-knockback attack is done.
When swimming, hitting something with a non-critical attack will make you stop swimming,