The Lord and his disciples had been out of Galilee for some time and so had no opportunity yet to pay the tax "of two drachmas" which was paid annually by Jewish men of 20 years of age or more in the month of Adar (March), for the maintenance of the temple in Jerusalem.
Tax collectors came and asked Peter if his Teacher did not pay the tax. Peter said Yes. It is curious that they were asking the disciple rather than the Lord Himself - could it be out of respect, or to avoid embarrassment? They were tactful. From what comes next, we understand that the very religious Peter had naturally thought the Lord had the duty to pay.
When Peter came into the house where the Lord Jesus was, He showed that He knew about the conversation, using a parable so Peter could see that, even as the family of the King of a country does not pay taxes, the Son of God does not have a duty to pay tax on His temple, the House of God.
Although logically exempt, in practice the Lord was willing to pay the tax "lest we offend them". The remaining Jews would not understand this logic, and the Lord chose to do what He need not do, in order not to give the impression that He and His disciples despised the temple, and the worship that was done in it according to Mosaic Law.
He therefore immediately provided the necessary amount for tax for both of them out of the mouth of a fish that on His instruction Peter found in the mouth of a fish, caught with a hook. With this miracle, He proved His divine attribute of prescience: He knew which one of all the fish in the Sea of Galilee had a coin of the correct value in its mouth, where it was, and that it would be the first to be hooked by Peter.
It is an example to be followed, when we are confronted with habits and customs we are not obliged to keep, but with which we comply in order not cause offence, provided they are morally innocuous.
24 When they had come to Capernaum, those who received the temple tax came to Peter and said, "Does your Teacher not pay the temple tax?"
25 He said, "Yes." And when he had come into the house, Jesus anticipated him, saying, "What do you think, Simon? From whom do the kings of the earth take customs or taxes, from their sons or from strangers?"
26 Peter said to Him, "From strangers." Jesus said to him, "Then the sons are free.
27 Nevertheless, lest we offend them, go to the sea, cast in a hook, and take the fish that comes up first. And when you have opened its mouth, you will find a piece of money; take that and give it to them for Me and you."
Gospel of Matthew chapter 17, verses 24 to 27