I have always been taught that Jesus got baptized as an example for us. If people were getting baptized before Jesus did, then why did he need to make that an example if people were already being baptized?
Since you did not ask for a particular denomination's viewpoint, please allow me to give a Catholic perspective to this question.
The Baptism of Jesus marked the beginning of His public ministry and the wanted the faithful to not only to have an example, but He also wish to share to all our humanity at the same time and show us the way to salvation. God also used this occasion to anoint Jesus of Nazareth with the Holy Spirit and power.
What does the Gospel tell us about this event:
Then Jesus came from Galilee to John at the Jordan, to be baptized by him. John would have prevented him, saying, "I need to be baptized by you, and do you come to me?" But Jesus answered him, "Let it be so now; for it is proper for us in this way to fulfill all righteousness." Then he consented. And when Jesus had been baptized, just as he came up from the water, suddenly the heavens were opened to him and he saw the Spirit of God descending like a dove and alighting on him. And a voice from heaven said, "This is my Son, the Beloved, with whom I am well pleased." - Matthew 3:13-17
Now let us see what St. Thomas has to say on thi subject.
St. Thomas Aquinas, in the Summa Theologica (see ST 3, 39, 1-8), listed a number of objections, including, “It would seem that it was not fitting for Christ to be baptized. For to be baptized is to be washed. But it was not fitting for Christ to be washed, since there was no uncleanness in Him. Therefore it seems unfitting for Christ to be baptized.” His answer draws upon both Scripture and the Church Fathers: “I answer that, It was fitting for Christ to be baptized. First, because, as Ambrose says on Luke 3:21: ‘Our Lord was baptized because He wished, not to be cleansed, but to cleanse the waters, that, being purified by the flesh of Christ that knew no sin, they might have the virtue of baptism’.”
He also quoted St. Gregory Nazianzen, who had written, “Christ was baptized that He might plunge the old Adam entirely in the water.” He further noted that Jesus, in being baptized, was setting an example for everyone who would follow him and take up the cross of discipleship. After all, if the sinless Son of God would willingly enter the waters of baptism, how much more urgent is it that we sinners are washed in the waters of regeneration?
There are other reasons given, including one implicit in the sermon of St. Peter to the first Gentile convert, Cornelius, heard in today’s reading from the Acts of the Apostles: “You know the word that [God] sent to the Israelites as he proclaimed peace through Jesus Christ, who is Lord of all, what has happened all over Judea, beginning in Galilee after the baptism that John preached, how God anointed Jesus of Nazareth with the Holy Spirit and power.”
Here the emphasis is on the anointing; the Greek word is echrisen (ἔχρισεν) and the root word is chrió, from which comes the word “Christ”. And the title “Christ” is itself the Greek translation of the Hebrew word, “Messiah”, which means “anointed”. The Catechism explains that this is “the name proper to Jesus only because he accomplished perfectly the divine mission that ‘Christ’ signifies. In effect, in Israel those consecrated to God for a mission that he gave were anointed in his name. This was the case for kings, for priests and, in rare instances, for prophets. This had to be the case all the more so for the Messiah whom God would send to inaugurate his kingdom definitively” (par. 436). Jesus was anointed by the Holy Spirit and presented as uniquely fulfilling the threefold office of prophet, priest, and king. - Was it fitting and necessary for Jesus to be baptized?
Christ Jesus was baptized to "fulfill all righteousness". What this means is He did not appoint Himself as high priest; He did not take this honor by Himself. Rather, like Aaron, God appointed Him.
And Jesus answering said unto him [John the baptist], Suffer it to be so now: for thus it becometh us to fulfil all righteousness. Then he suffered him. Mt. 3:15
And no man taketh this honour unto himself, but he that is called of God, as ]was Aaron. Heb. 5:4
Jesus answered, If I honour myself, my honour is nothing: it is my Father that honoureth me; of whom ye say, that he is your God: John 8:54
So also Christ glorified not himself to be made an high priest; but he that said unto him, Thou art my Son, to day have I begotten thee. Heb. 5:5
And lo a voice from heaven, saying, This is my beloved Son, in whom I am well pleased. Mt. 3:17
Back in Exodus when the Israelites were in Egypt about to be set free, God called Moses. After the Israelites rejected to hear the voice of God (Ex. 20:18-19), God then told Moses to call Aaron and his sons. They would be priests in the Levitical order to stand before God in the stead of the children of Israel. To appoint them publicly, Moses was to wash (baptize) them.
And Aaron and his sons thou shalt bring unto the door of the tabernacle of the congregation, and shalt wash them with water. Ex. 29:4
That OT example of a calling by God was the pattern for the NT reality whereby John the baptist washed (baptized) Christ, thus appointing Him as high priest of the Melchizedek order.
Moses >>> Aaron >>> high priest >>> Levitical
John >>> Jesus >>> high priest >>> Melchizedek
That is why Jesus Christ was baptized.