It may lead to additional questioning, but provided you have the appropriate visas / right to access in both countries, there's no theoretical reason you can't.
It's very common at Victoria Falls, where people hop back and forth between Zimbabwe and Zambia on the bridge at the border crossing for photo opportunities, or to get to the bungy jump in the middle of the bridge.
I've spent less than a couple of hours in Paraguay, and less than that in Brazil twice (getting to a border town there from Argentina).
I've walked across to the US from Canada to get dinner, and then walked back a couple of hours later too (that one raised eyebrows but was fine).
I've also bussed into Mexico merely to fly out to Cuba, and I've spent a couple of hours in Lithuania once between flights.
So it's not uncommon
, I've never had problems, and legally there's nothing stopping you, unless your reasons for doing so raise concerns with the border guard / officer.
travel.stackexchange.com/users/101/Mark Mayo
In the Netherlands we have "het Drielandendpunt" (the three-country point).
That is where Germany, Belgium and the Netherlands share borders.
You can be in three countries at the same time and nobody cares how many circles you make running around the boundary post.
The green lines are actual country borders.
travel.stackexchange.com/users/38276/Pieter B
At several borders there has been a practice called "Visa Run".
People would renew their "visa on Arrival" by briefly passing the border to the neighboring country.
Examples are e.g. the Turkish-Greek or Turkish-Bulgarian or the Thailand<-> Cambodia, Malaysia, Laos Borders.
travel.stackexchange.com/users/18486/arved
I do this frequently in Canada.
Many Canadian airports have no concept of international transit so when flying from the US to Europe or Asia you need to enter and then immediately leave again.
It's quite inconvenient but also sort of funny: the immigration officer often asks "how long do you plan to stay in Canada?"
and I'm always tempted to answer: "only 45 minutes but its going to be a lot longer if you don't hurry up"
travel.stackexchange.com/users/13971/Hilmar
The common point in the other responses that's not made explicit, is that it is never a problem to stay for an arbitrary small amount of time in one country.
What might give you problems is returning to the country you came from.
Obviously, countries with open borders typically won't mind at all.
But, if you're hopping borders for a visa run, laws might be in place that will deny you entry, or might see you be hassled by immigration officials of the country you return to.
So, because different visa rules apply to different people in different countries, leaving some country for a very short amount of time and then returning might be problematic.
However, staying in some country for a very short time should never be a problem.
travel.stackexchange.com/users/2271/MastaBaba
I was once in international transit in Germany, and for reasons I do not remember, I needed or wanted to get out to the train station or some other part of the airport.
I got lost, so I went through passport control three times instead of once to get out of the international zone.
In other words, I entered Germany, exited a couple of minutes later, and then entered again a couple of minutes after that.
Then I exited once again a few hours later for my flight.
There was no trouble or delay.
travel.stackexchange.com/users/19400/phoog
There is no minimum time you must spend; I know many people that fly Kuwait - Bahrain - Kuwait (just to reset visa time limits).
They spend less than 15 minutes in Bahrain
(that is, time spent from when the visa is stamped entry, till exit stamp).
They actually spend more time in the lounge than "officially" in the country.
Similarly, I have done a "day run" to Dubai where I fly out and return the same day.
No issues whatsoever.
travel.stackexchange.com/users/1375/Burhan Khalid
I spent 30 minutes in Dubai this year.
I went through immigration, out to the mosque in the airport parking lot, found and logged a geocache , and went right back into the airport and through immigration.
travel.stackexchange.com/users/12690/Sumyrda - remember Monica
I guess it is dependent on the pair of countries.
In most of the European Union there is no border control (most of the times) within the Schengen Area .
So you may just go across and back.
travel.stackexchange.com/users/7687/Rolazaro Azeveires
I would say it is very much dependent on the specific case.
Check the border in this question on Stackexchange.
You might get problems if this quick in-and-out looks as if you're trying to exploit a loophole in the law.
travel.stackexchange.com/users/27778/o.m.
A road runs along a good portion of the border between Brazil and Uruguay and crosses the border many times.
It goes through the cities of Rivera and Santana do Livramento where for about ten kilometers, the street is the border.
You can cross that street as often and as fast as you want (if drivers don't stop you).
travel.stackexchange.com/users/12555/WGroleau
Some countries do have different visa requirements for folks who are just passing through (transit visas) versus visas to actually visit the country.
Some airports are better than others about handling changing planes without technically leaving the "international" space.
travel.stackexchange.com/users/11680/keshlam
For the minimum duration in country (requiring a visa, rather than the many options that do not require a visa, such as The Hotel Arbez )
it may be hard to beat the example at Turkish single-entry visa stamped in transit by mistake.
Can I still use it to enter Turkey? .
OP does not specify the time interval there but by the sounds of it
it was a few seconds rather than a few minutes.
travel.stackexchange.com/users/4995/pnuts
Is there a minimum amount of time I must spend in a country ?  Is this country dependent? Or I can just enter a country and then exit immediately in 15 minutes or so?
Can I enter and exit a country immediately?
travel.stackexchange.com
<customs-and-immigration><international-travel>
travel.stackexchange.com/questions/60260
travel.stackexchange.com/users/17105/Yash Desai
You might get problems if this quick in-and-out  looks as if you're trying to exploit a loophole in the law, but this is often done and can be just to reset visa time limits. There is no minimum set time to stay in one country. It is not generally considered a problem to stay a very short time in one country. In fact some countries are regularly visited in this way. However, if you appear to be doing something illegal, you may get into difficulties.
Some countries are often used for very short entry times: Canada, Bahrain and Dubai are examples. It is never a problem to spend a very short time in a country. There may be problems depending on the countries involved and if you look as if you are doing this for illegal reasons.