First, lets talk briefly about the underlying issue, before addressing your specific question:
If you get serious muscle pain (taking 5 days to go away is serious), you should adjust your hiking. Apart from the general measures, like adjusting distances and trying to get weight off your pack, for longer hikes like this you also need to think about rest days. In youth mountaineering courses I was taught a concept that's mainly geared towards injury prevention which loosely translates to "mid-week-low". It is pretty well observable, that in the middle of a longer trip there is an over-proportional decrease in the general disposition on that day, which also increases injury risk on it. If you are doing an activity for more than 5 days, a rest day is very important. A rest day doesn't mean you need to sit around all day, doing nothing but lying in the sun and maybe some stretching (though there is nothing wrong with that either), but you need to give your body a longer time of lower exertion. Maybe just do a much shorter distance that day, walking slower, taking time for extended rests. It doesn't really matter what you do, as long as it is a lot less intensive (and you enjoy it).
Now that's all good and nice, but what do you do if you nevertheless are in the situation, where your muscles ache a lot. Sorry to disappoint, but once you are at this point, you likely wont get rid of the pain while continuing the hike. The only real remedy is rest. Massaging helps, either by yourself or from a potential hiking partner. If the aches are really bad, I personally don't stretch - it seems to make it worse. If you can shorten the distance, obviously adjust them down.
Again what's important is prevention on the go: If you feel pain developing while walking, take a rest (i.e. before it gets bad). Massage and lightly stretch the muscle. Important: We are not talking about flexibility training here - you neither need to stretch very hard nor for a prolonged time, listen to the feedback your body provides. Then you need to adjust pace. I know it's hard, especially if you have a set goal and/or hiking partners, that get slowed down. There is no point ignoring the problem and then potentially having to abort the hike entirely. If they don't have any problems and have reserves, maybe you can off-load some of your weight - that goes a long way. It might take a bit of guts to ask (if they didn't offer already), but there's really no shame in it. Maybe they are even relieved, because they hesitated to propose it themselves.