This is called animal instinct.
Animals are not rational like man, but are driven by their survival instincts.
Thus it is amazing how some animals are born and already know how to hunt or take steps to protect themselves. In the case of ducklings, the instinct causes them to be born and connect to the nearest object, because instinct makes them believe that it is the mother duck.