Jesus’ baptism has always been a bit of a head-scratcher to me.
Jesus was baptized by a man called “John the Baptist,” who preached, “Repent, for the kingdom of heaven is at hand!” (Matthew 3:1-2).
The people John baptized confessed their sins (Matthew 3:6) because John proclaimed “a baptism of repentance for the remission of sins” (Luke 3:3).
The Bible proclaims that Jesus never sinned (2 Corinthians 5:21; 1 Peter 2:21-24), so He did not need to repent or have any sins forgiven.
So why was Jesus baptized by John?
Jesus was baptized to fulfill all righteousness.
John actually tried to stop Jesus from being baptized. He said to Jesus, “I need to be baptized by You, and are You coming to me?” (Matthew 3:14).
Jesus responded to John, “Permit it to be so now, for thus it is fitting for us to fulfill all righteousness” (Matthew 3:15).
I believe the following quote from H. Leo Boles to be helpful.
Nothing must be left undone that would honor God and assist Jesus in beginning and carrying on his ministry. As John’s baptism was not “from men” but “from heaven” (Matthew 21:25; John 1:33), it became [was fitting for] Jesus to receive the baptism of John, and John to administer it. It was fitting that Jesus should fill up the full measure of righteousness in all its forms by accepting the baptism of John. - Boles, The Gospel According to Matthew, 91.
Although Jesus never sinned, He took the place of sinful mankind (2 Corinthians 5:21). By being baptized with John’s baptism, Jesus associated Himself with sinful humanity - not because He was sinful, but because He would fulfill the prophecy as the one who would carry our sins.
Surely He has borne our griefs And carried our sorrows; Yet we esteemed Him stricken, Smitten by God, and afflicted. But He was wounded for our transgressions, He was bruised for our iniquities; The chastisement for our peace was upon Him, And by His stripes we are healed. All we like sheep have gone astray; We have turned, every one, to his own way; And the LORD has laid on Him the iniquity of us all. (Isaiah 53:4-6)
I believe Jesus saying He must fulfill all righteousness can be connected with His words about fulfilling the Law and the Prophets.
Do not think that I came to destroy the Law or the Prophets. I did not come to destroy but to fulfill. (Matthew 5:17)
Jesus was not baptized because of His own sins. He was baptized to fulfill what the “Law and Prophets” said of Him - the “Lamb of God who takes away the sin of the world” (John 1:29).
Jesus was baptized to reveal Himself as God’s Son.
In the gospel of John (John the apostle wrote it, not John the Baptist), another reason is given for Jesus’ baptism.
And John [the Baptist] bore witness, saying, "I saw the Spirit descending from heaven like a dove, and He remained upon Him. I did not know Him, but He who sent me to baptize with water said to me, 'Upon whom you see the Spirit descending, and remaining on Him, this is He who baptizes with the Holy Spirit.' And I have seen and testified that this is the Son of God." (John 1:32-34)
After Jesus was baptized, the Holy Spirit descended like a dove and came to rest upon Jesus (Matthew 3:16). At this moment, “a voice came from heaven, saying, ‘This is My beloved Son, in whom I am well pleased’” (Matthew 3:17).
John the Baptist was the one who prepared the way of the Lord (Matthew 3:3). At first, he didn’t know who the Messiah - or Christ - would be.
When all the things happened after Jesus’ baptism, it confirmed to John that Jesus was the Christ, the Son of God.
Today, we are no longer baptized into John’s baptism (Acts 19:1-5). Now, baptism is done in the name (by the authority) of Jesus for the forgiveness of sins.
Then Peter said to them, "Repent, and let every one of you be baptized in the name of Jesus Christ for the remission of sins; and you shall receive the gift of the Holy Spirit. (Acts 2:38).
Because of the blood shed by Jesus - the Lamb of God and the Son of God -our sins can be washed away by His blood (Acts 22:16; Revelation 1:5).