The Apollo CSM was not originally conceived solely for the lunar landing mission, but rather as a general purpose spacecraft for NASA's long term plans which included crewed space stations in Earth orbit among other things. The CSM would be ferrying station crews, so a 3-person ship would obviously be better than a 2-person ship for that role.
Prior to the Apollo flights, another rationale for having a 3-person crew was a desire to have someone on watch around the clock, but the Gemini and Apollo crews quickly found that it was difficult for one person to sleep while another crew member was busy in the cramped command module. Instead, the crew normally took rest periods together, with Earth-side mission control monitoring the spacecraft's systems.
The rendezvous and docking after departing the lunar surface was, if I understand it correctly, carried out with the LM's thrusters.
The rendezvous and approach was done by the LM, but the final docking maneuvers were performed by the command module pilot. The CM's docking hatch was forward, so the CMP could easily see what was going on, while the LM's hatch was on the cabin roof; the commander would have an awkward straight-up view during the docking.
It was possible for the commander to fly the docking from the LM if the CMP was incapacitated or the CSM's thrusters inoperable, but that was never done in practice.
Conversely, once the LM was in a stable lunar orbit, the CMP could fly the CSM to rendezvous if the LM wasn't able to -- another advantage to having a crew member in the command module.
Were some of the CM maneuvers (e.g. reentry?) so complex that one of the two moon-walking astronauts couldn't have crammed for them? Or were there planned contingencies where the CM pilot would have swapped into one of the other two roles after launch?
Any of the CMP's maneuvers could have been flown by the commander; both trained to fly the command module. However, like the Lunar Module Pilot title, "Command Module Pilot" conceals the fact that the role includes numerous systems-management tasks. The CSM and the LM were each complex spacecraft with a lot of things to take care of; the commander had to be comfortable with both, but having a separate crew member being deeply familiar with each ship freed the commander up to deal with high-level issues instead of the position of every environmental-control systems switch.
It certainly would have been possible to design a 2-crew lunar mission. In fact, there were serious, workable proposals to do a Gemini-based lunar landing, and the Soviet counterpart was a two-crew orbiter, one-crew lander system. However, I think most people involved in the development of Apollo probably saw the three-person crew as providing valuable redundancies.