During drydock, they can service the underside of the ship, which they can't do while the ship is afloat. Also, they can do major construction/renovation projects on board, which couldn't be done during a cruise. Something like replacing the carpeting in a dining room, for example.
Most of the maintenance can be and is done while the ship is afloat. Some of it cannot, so the ship must be taken out of service on occasion. Usually every two years or so.