Originally the Holy Roman Emperor would give a Markgraf an area of the borders (the Marches or German: Mark) to look after. The Margrave would have to protect the people in the area from invaders, and at the same time protect the empire.
A Markgraf ranked higher than a graf, but lower than a duke. The Margraves of Brandenburg were in control of such an important area that they were made Electors of the Holy Roman Empire, which was the highest honour in the Imperial Parliament. There were no Kings in the Empire, so when the Margraves of Brandenburg took control of some territory outside the empire as well, they could call themselves King in Prussia.
As the Middle Ages ended and Europe and the Holy Roman Empire became more secure the military importance of the Margraves became less. At the end of the German Empire in 1918, there were no Markgrafs left. All had been given higher title, but may have kept the Markgraf title as an extra. The Kings of Prussia, for example, always kept the lower title Margraves of Brandenburg.