There are several reasons why Jesus was baptized. I will explain two reasons which will include Jesus' answer to John as to what " to fulfill all righteousness means" (Matt 3:15).
First, it was Christ's public anointing as King. John the Baptist (JTB) came in the spirit of Elijah, but it's hard to miss the similarities of his and Samuel's ministry. Author A. W. Pink alludes to this in his book The Life of David Vol I:
It is a remarkable fact that David was anointed three times. First,
privately at Bethlehem (1 Sam. 16:13). Second, by the men of Judah (2
Sam. 2:4). Third, by the elders of Israel (2 Sam. 5:3). So also was
that august One whom he foreshadowed. This will appear the more
evident if we quote the following: "Then Samuel took the horn of oil,
and anointed him in (or "from") the midst of his brethren: and the
Spirit of the Lord came upon David from that day forward" (1 Sam.
16:13). Concerning our Lord, His humanity was miraculously conceived
and sanctified by the Spirit and endowed with all graces in the
Virgin’s womb (Luke 1:35). Second, (see Isa. 6) He was publicly "anointed with the Spirit" (Acts 10:38) at His baptism, and thus equipped for His
ministry Is 61:1). Third, at His ascension He was "anointed
with the oil of gladness above His fellows" (Ps. 45:6, 7).
Just as the priest Samuel anointed an unknown and seemingly weak shepherd named David from Bethlehem as king of Israel, JTB (who was also of priestly line) anointed the King of Kings. What should grab our attention are the subsequent events immediately after Jesus is baptized. John sees the Holy Spirit descend upon Jesus, and he hears the Father's voice identify Jesus as His Son. The same son who is decreed a kingdom in Psalms 2:7:
Psa 2:6 Yet have I set my king upon my holy hill of Zion. Psa 2:7 I
will declare the decree: the LORD hath said unto me, Thou art my Son;
this day have I begotten thee. Psa 2:8 Ask of me, and I shall give
thee the heathen for thine inheritance, and the uttermost parts of the
earth for thy possession.
A second reason why Jesus passed through the waters of baptism, and one more specific to Matthew's account of, and aim of his gospel, which was to represent Jesus recapitulating the historical events of OT Israel. We find Matthew using the fulfillment formula throughout his book. For example, Matt 2:15 reads:
Matt 2:15 and was there until the death of Herod; that it might be
fulfilled which was spoken by the Lord through the prophet, saying,
“Out of Egypt I called my son.”
Matthew used a portion of scripture from Hosea 11.1, which textually applied to the nation of Israel and applied it to Christ. Theologians differ as to the reasoning of the fulfillment passages, but most agree that Matthew's gospel goes through great lengths to prove Christ as the preeminent Son of all creation, One whose life was foreshadowed in Israels history as people, places, and objects. Consider these passages when you have a moment: Matt 12:49. Jn 1:14, 2:19, 3:14, 6:35.
In Matthew we find Jesus fully identifying with the nation He came to save by undergoing the OT ordinances like circumcision as a child, (Luke 2:21) and abiding by OT law and customs. He passes through the waters of baptism fulfilling the second exodus motif found in Isaiah chapters 40-55, and just as OT Israel passed through the Red Sea to identify with their mediator Moses (see 1 Cor 10:2) Jesus undergoes baptism because he was the way of salvation. True, Christ is sinless, but he identifies with sinful man by dying which only sinful man short of God's grace should do.
Following baptism, Matthew records Jesus immediately going through the wilderness for forty days to be tempted, just as Israel was put to the test for forty years. Instead of failing to reach to kingdom as the first generation of wilderness bound Israelites after the exodus, Jesus defeats Satan by the word of God (interestingly by using verses from Deuteronomy) and seals the entrance into His kingdom by His death, burial and resurrection. So Jesus' response to JTB in regards to fulfilling righteousness was about completing the righteous requirements set forth by His Father to obtain the throne that was His before the earth's foundation. Christ not only righted the wrong of Adams sin, but He righteously fulfilled the failings of Israel. This was to prove himself as the "firstborn" of creation (Col 1:15). The true Son of God. Meaning not physically born first according to natural order, but the Son who would receive His Father's inheritance by promise (Gal 3:16).
.