It is the first fall in the rate since October 2016 and was largely down to lower petrol and diesel prices.
Fuel prices fell for the fourth month in a row in June, according to the Office for National Statistics.
Economists say the fall in inflation could ease pressure on the Bank of England to raise interest rates.
The UK inflation rate has risen sharply since the referendum on membership of the European Union last June, partly due to an increase in the cost of imported goods following the fall in the value of the pound.
"These numbers are a real surprise, showing the first drop in inflation since autumn 2016," said Lucy O'Carroll, chief economist at Aberdeen Asset Management.
"This is going to kill the chances of a rate rise in the short term. We'll learn more about the Bank of England's thinking in a couple of weeks, but we can expect the calls for a  rate rise to reduce to a whimper."
But June's fall in inflation is a blip, according to some economists.
"We have not necessarily passed the peak of inflation," said Andrew Sentance, senior economic adviser at PwC.
"Oil prices are volatile and could bounce back later this year. Meanwhile, the big fall in the value of the pound since last summer is still working its way through the pipeline and has not yet fully fed through into shop prices."
Another measure of inflation, the Retail Prices Index (RPI), edged lower from an annual rate of 3.7% in May to 3.5% in June. RPI is used to calculate the interest rate for student loans, for example.
The ONS also reports a version of consumer price inflation that includes housing costs known as CPIH. That rate fell to 2.6% in June from 2.7% in May.
Also contributing to June's fall in inflation was a decline in prices for recreational and cultural goods, which includes items such as toys, computer games and sports events.
Despite June's fall, inflation is still running ahead of average wage growth, which stands at 2% excluding bonuses.
That is eroding the value of people's pay, something the Bank of England has been keeping a close eye on.
Last month, the Bank's governor, Mark Carney, said that "anaemic" wage growth one of the reasons why he was not supporting a rise in interest rates.
While Mr Carney is not keen to raise interest rates, other members of the Monetary Policy Committee think the time has come to make a move.
In last month's meeting three members out of eight voted in favour of a rate rise from the current level of 0.25%.
Inflation has been on a steady climb since the end of 2015 and accelerated after the EU referendum result in June last year, as the sharp fall in the value of the pound pushed up the price of everything we import.
A bit like a snake that has swallowed something big, that bulge in import costs has taken its time to work through the economic system because producers, wholesalers and retailers order things in advance, so it takes time for higher costs to show up in consumer prices.
Inflation could still go up again this year - and many economists forecast it will - but it seems likely that we have been at least close to the peak.
More from Simon
Enter your details below. Source: Office for National Statistics.

The incident happened at the junction with the B937, near Collessie, at about 21:05 on Saturday.
Police have appealed for witnesses to the accident which involved a silver Volkswagen Beetle.
Sgt James Henry, of Police Scotland, said: "Anyone who may have seen a man walking in the area of the A91 around that time on Saturday, or who witnessed the incident, please get in touch."
The UK's inflation rate dropped unexpectedly to 2.6% in June, down from 2.9% in May, official figures have shown.
[NEXT_CONCEPT]
A man has died after being hit by a car on the A91 in Fife.