If God forgives our sins at baptism, why does 1 John 1:9 say we are forgiven of our sins by confessing?
1 John 1:9 does not say that we are forgiven of our sins by confessing. This is an incorrect interpretation of this passage. What John is saying is that “If we walk in the light, as he is in the light, we have fellowship with one another, and the blood of Jesus, his Son, purifies us from all sin.” (1 John 1:7). If we have been saved, then we need to continue to walk in the light. If we do, we will remain saved and God will forgive all of our sins. Part of this lifestyle of “walking in the light” is that we must continue to confess that we sin. If we confess that we are sinners, he will continue to purify us of all sins. It is not the confession that produces forgiveness. It is the blood of Jesus and our initial repentance and baptism that gets us forgiven. John is talking about remaining in Christ, not how to be saved in the first place. We as Christians are already forgiven if we are saved. However, John is writing so that we can be sure of our salvation–that we can stay saved and finally make it to heaven (1 John 5:13-14). I suggest that you read the entire book of 1 John and try to understand the general thrust of the book, rather than take one verse out of context, as this can lead to incorrect conclusion.
I hope this helps.