When you put the pen cap in the water a bubble of air
is trapped inside it. This
trapped air bubble makes the diver
less dense than water so it floats.
When you squeeze the
bottle, water squashes the air and
takes up more space in the pen cap.
Now the diver is denser than
water so it sinks.
When you relax your
grip on the bottle, the air in the
pen cap expands again. Now
the diver is less dense than water,
so it comes back up to the surface.