Why did Jesus get baptized by John if he had no sin?
This is a very good question. Jesus actually answered the question for us, although his answer is a bit enigmatic. To quote Matthew 3:15, "Let it be so now; it is proper for us to do this to fulfill all righteousness." Of course the question is what did Jesus mean by this? Note, he did not say he did this in order to attain righteousness. Jesus already was righteous as a man without sin. He did this to fulfill righteousness. I see a parallel to the claim of Jesus that he would fulfill (complete, bring to an end, bring to its fullness) the Law of Moses (Matthew 5:17). Exactly how Jesus fulfilled the requirements for righteousness, I am not sure, to be honest, although I will supply some suggestions below. One thing we can be sure of, which is implied in your question is that Jesus was not baptized on order to be forgiven of sin. Let me suggest a few reasons that Jesus submitted to baptism.
1. This marked the "annointing" of Jesus. This is when the Holy Spirit came on Jesus in a special way. We do not know exactly what this means. In other words the Bible does not make it clear exactly how Jesus was changed when the Holy Spirit came upon him when he was baptized by John, but we have to assume that this was a very significant moment in the life and ministry of Jesus. As it says in Matthew 3:16, it was at that moment that the Spirit of God descended on Jesus like a dove. This marked the beginning of the public ministry of Jesus. This idea is enhanced by John 1:32-34. John the Baptist told the people that God had told him the one on whom the Spirit comes is the Son of God. This annointing of Jesus was proof to John the Baptist, if not to others, of who Jesus was.
2. This event was the fulfillment of a prophecy in the form of a prefigure. The prefiguring event happened in 1 Samuel 16:13. When Samuel annointed David as scene which is a clear prefigure of the baptism of Jesus occurred. Like David, the prefigure of the Messiah, on whom the Spirit came at his announting, the Spirit came upon Jesus when he was annointed in his baptism.
3. The baptism of Jesus served as a gesture of humility and submission of Jesus to God and to the ministry of John the Baptist. This showed the humanity of Jesus and his submission to the will of God.
4. The baptism of Jesus serves as an example to others. Jesus asked all those who would follow him to be baptized in the name of the Father, Son and Holy Spirit (Math 28:19). Jesus did not ask us to do something he was not willing to do. He did not need to be forgiven but he did need to demonstrate his submission to God and he did need to set an example to all of us in being baptized.