I think it was a theophany, an appearance of Jesus, pre-incarnation, the commander of His Father's army.
If it were an angel, he would say, "Don't worship me." And the place is holy because of Who is there with Joshua.
But the main point of the passage is to show that God is in charge, and Joshua is under Him. The point is to put Joshua on par with Moses, as the same thing happens at the burning bush.
It is a great example of literary foreshadowing: we don't fully understand what this means until several chapters in the book (several books later in the Bible) when Jesus comes - really not until we see Him as the commander of the army in Rev 19:11-16!