Would 4 or 5 nights be sufficient to see the Rhine and Mosel Valleys?
That sounds reasonable for the average visitor with modest sightseeing goals.
We also want to see a bit of the Mosel Valley and visit Burg Eltz. We aren't sure whether we should do those as day trips from Bacharach, or if we should move to Cochem for a night or two and visit from there.
Splitting your time between the Rhine and Mosel makes sense to me.
We will start in Berlin, go to Dresden for a couple of days and then take a train west. Should we pick up a rental car in Frankfurt or take a train from there to Bacharach which will be our base for a few days?
It would not be my choice to rent the car in places where a car isn't necessary and where train travel is offered free of charge. The Rhine/Mosel region and the Black Forest are well-served by train and offer guest tickets for free transport if you stay in the right places.
The are good train trips from Dresden to the Rhine towns every two hours at 8:10, 10:10, and 12:10. The ICE from Dresden is direct all the way to the Rhine (Mainz, a 5-hour journey.) Transfer in Mainz to a local train for the smaller Rhine towns.
There is a wide area you can explore by train for free - see this Guest ticket guide with rail map with Koblenz at the center - if you make your stay in Boppard, north of Bacharach on the same rail line. And that is what I recommend. Boppard is a great town, too. Lots of restaurants and hotel choices, with outdoor cafes and eateries on the market square and many on the waterfront (Bacharach has nothing with river views, sadly); handsome half-timbered buildings; an awesome chairlift ride that takes you to the clifftops for nice views; several interesting sights in town and proximity to St Goar (Rheinfels Castle,) Oberwesel, and Bacharach. You can also use the Boppard ferry crossing to get to Filsen station for travel on the opposite riverbank to Rüdesheim or to Braubach (home of Marksburg Castle, never-destroyed medieval castle.) Boppard has a KD boat dock as well (and you can take KD boat directly to Braubach from there if you prefer that to the ferry + train.)
Boppard info: https://www.boppard-tourismus.de/media/was-ist-wo-broschu__re_en_190218.pdf
The Guest ticket will get you over to Cochem on the Mosel when you leave Boppard. I would suggest dropping your bags at your Cochem hotel in the morning... then use your VRM Guest ticket right away to make the train + bus ride to Burg Eltz (
which takes about 50 minutes.) OR... you can take the old-school route to Eltz; train to Moselkern, then hike to Burg Eltz (a marvelous way to visit!)
In Cochem there are 11 different innkeepers who offer the VRM Guest card as well. If you aren't staying with one of them, there are still cheap day passes available for small groups like yours if you want to visit TRIER, or BERNKASTEL, or other places.w
In the Black Forest, there are more than a hundred different towns that offer the KONUS card, which also provides free-ranging travel on the railways there. See flyer and short video.
https://prospektbestellung.toubiz.de/media/prospekt/file/5736289_KONUS_GB_2022.pdf
https://www.youtube.com/watch?v=KkgzMXQ7blQ
If you want to use the car from the Black Forest to the German alps and Munich, there are places where you can pick up a car, for sure. Many on this forum have used Gengenbach as a Black Forest base town - which works well for train outings but is also close to Offenburg, where you should be able to pick up a rental car.