The piece modulates from G minor to Bb major between the bars 12-14 but I don't know how to mark these chords as they have B natural in them, which doesn't belong neither to G minor nor Bb major. So, what function does it actually have?
Hmmm, this is a little off the cuff, but I think it's probably easiest to look at mm. 12-13 (primarily 13) as a viio/ii or V7/ii in the new Bb key. That is to say, the B-nat is part of a secondary leading-tone or secondary dominant chord. I suppose it could just as easily be a viio/iv or V7/iv in the old key, and then you could call the iv a pivot chord like you already have. Probably it's six of one, half a dozen the other.
Effectively it's a G dominant 7th leading to a C min. in the next bar. The Eb note works like an augmented 5th,(as in D#), pushing even more to the iim as in Cm. So I'd call it a dominant to the iim in bar 14.
Bar 13, now I've played it, could even be construed as an Fo, called Vo in this part going to Bb, or VIIo if you think it's still in Gm at that point. Putting in the 'missing' Ab sounds o.k. to me, and I think Bach used this sort of chord to modulate on occasions.
The B-natural in m. 12 is an anticipation of the B-natural in the next bar. The chord in m. 13 is the dominant 7th of the pivot chord that you correctly indicated on the score. V7/iv for the m. 13 chord.
Discussion of what the composer was thinking is impossible. Whether or not there were Roman numerals at the time, or something similar, is also conjecture. The point is the composer knew the same as we do, just in a different way.
You are right to label bar 14 as ii V I in B flat major.
Bar 13 is a secondary dominant of ii, i.e. a G dominant 7th chord, or B diminished if you prefer to call it that because the only G is the passing-note in the treble.
The bare octave D at the start of bar 12 is functioning as a dominant of G minor. But this piece isn't 4-part common-practice 18th-century harmony, it's two-part counterpoint. The D-C-B-A in the bass of bar 12 are a natural way to get from an (inplied) D chord to the G7 in bar 13.
Trying to decide "exactly" where transition occurs between the D chord reduced to only one note (D) and the G7 is a bit like trying to decide "exactly" where red changes to orange when looking at a rainbow - i.e. it's not a very profitable question to ask.
An 18th or 19th century analysis would probably have called bars 12-13 a modulation into C minor en route to B flat major in bar 14, but that would not be the 20th or 21st century interpretation.
On the evidence of CPE Bach's textbook "the true art of playing keyboard instruments", which contains a long section on improvising a keyboard accompaniment from a figured bass part, it is clear that Bach didn't think in terms of "chords", but rather in terms of intervals above the bass part. Harmonic progressions in the modern sense were an "emergent property" of the counterpoint, not the other way round. In 1722, Rameau wrote his own textbook with the revolutionary idea that the notes C-E-G and E-G-C were really two versions of the same chord, and not two different sets of intervals (5-3 and 6-3) above two different bass notes (which of course corresponds with current thinking about harmony). But even if the 40-year-old JS Bach was aware of Rameau's book, probably had little influence on his thought processes.
The B-natural leading tone temporarily tonicizes C, by implying a
G is NOT just a passing tone in the right hand, it is the bass-note on the 3rd beat! That's a G7 in 1st inversion, resolving to an incomplete 1st inversion Cmin (C-Eb) chord on the next down beat: it's screaming G7-Cmin. Even the F (beat one bar 13) resolves properly to the Eb. I didn't play/hear this, but it looks like it would really heavily tonicize C.