This piece is somewhat of an anomaly in the oeuvre of Bach. So much so, that Albert Schweitzer stated it really could not have been written by Bach.
Nowadays it is assumed that it is really J.S. Bach who wrote it, but in an early period of his life, around 1704.
The manuscript is was taken from has been compiled by Walther from works by various composers. Walther's handwriting is also recognized having written JSB as the composer of this piece.
There is only one piece of the North German school in about the same style, written by Kuhnau in his first Biblical Sonate, where he uses this form to let the oppressed Hebrew people sing to the tune of "Aus tiefer Not schrei ich zu dir".