It's called behavioral conditioning. The fish probably doesn't remember what you did to punish it, but it associates the act it performed with your consequential negative stimulus. Using shaping techniques, a person can train pigeons to play ping pong, rats to play basketball and goldfish to swim through hoops. You can also convince brain-dead consumers to purchase whatever image you flash in front of their faces, but that's another story. It's not learning as we know it, it's a more primitive and devious technique that meddles with the survival instinct/pleasure centers of an animal.
I feel compelled to add that a man once conditioned two male bettas to live peacefully in the same tank. He sat at the tank for hours on end, poking the fish with the eraser end of a pencil whenever they would flare or nip. After three days, they stopped trying to fight and did live peacefully. Not that I'd recommend trying it. Seems a bit cruel overall.