I'd suggest that instead of letting him make the mistake and lose the plot, get him to focus on you
before he gets to that stage and reward for the behaviour that you want.
So set him up for success. For the meantime, to manage the behaviour (because you won't want it becoming a bad habit, or escalating) can you make a new route that doesn't involve yards with dogs? That way he can still get his exercise and you can work on the lack of focus separately.
What I'd do is find a yard with a dog that won't make the situation worse- if you know the person and the dog, even better. A dog that won't make the situation worse is a dog that isn't going to react if your pup loses the plot; you want a dog that's just basically going to ignore him, regardless of what your puppy is doing.
Get his focus
before you even start towards walking past the fence. Do rapid fire treats into his mouth- one or two for every step you take- so long as he's keeping his focus on you. You want to make it super duper rewarding for his eyes on you, not his eyes on the fence or the other dog. I wouldn't suggest doing the whole line of the fence (which is also why I suggest changing your walking route for the time being) first off. Just do a couple of steps, make a big fuss, walk back, end it on a high note. You can gradually increase the distance you walk past fenced dogs, as well as then working up to the different sorts of dogs behind the fence- you want to start with something quite uninteresting, like a dog that just sits off in the corner of the yard and doesn't pay attention at all, up to a dog that might be fence charging as you go past.
If he loses focus, I think you could approach it a couple of different ways. First you could freeze: don't do anything. Don't walk forwards, don't call him back, don't let him drag you to the fence. When he stops pulling and looks back at you- even for split second- mark it (clicker's best because of how precise it is, but if you don't use one your marker word will do) and run backwards, calling him. Make chasing you more exciting than chasing the dog. Reward him when he gets to you, take one or two steps back along the fence, reward for focus, end on a high note and don't push for so much the next session.
That waiting him out method won't work if he has no intention of looking back at you- or, no matter how hard you try, the dog behind the fence is, in your puppy's eyes, a million times more exciting than you at the moment. In that case I'd suggest a sudden pivot and direction change. When he catchs up, make a big fuss and reward him, then finish the session off on a high note with one or two steps of focus- and don't put too much pressure on him next time.
The main thing to remember is
set him up for success- you don't want him to fail, you want him to win.
It's a bit hard to tell from your post what he exactly wants to do with the dog behind the fence. Is it aggression? Play? Or is it simply a distraction he hasn't learnt to focus around yet?
If it's aggression, I'm going to shut up because I don't feel comfortable giving advice on that to other people
If it's just a lack of focus around distractions, hopefully the method/s I suggested might help a bit; I've used them to help with my dogs, with success.
If he wants to play, as an added reward- so long as you have enlisted the help of a friend and a friendly dog- you could incorporate the reward for focused attention on you and calm manners walking past other dogs as being able to play with the other dog. Just make sure you mix it up a bit, so he doesn't come to expect to
always get play time for walking past a dog- that could cause a few problems
Good luck with it, and I hope I've helped at least a little bit and not confused you

I can be a bit excessively wordy at times, hehe.